annotate toolboxes/FullBNT-1.0.7/KPMtools/optimalMatching.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 % MATCH - Solves the weighted bipartite matching (or assignment)
Daniel@0 2 % problem.
Daniel@0 3 %
Daniel@0 4 % Usage: a = match(C);
Daniel@0 5 %
Daniel@0 6 % Arguments:
Daniel@0 7 % C - an m x n cost matrix; the sets are taken to be
Daniel@0 8 % 1:m and 1:n; C(i, j) gives the cost of matching
Daniel@0 9 % items i (of the first set) and j (of the second set)
Daniel@0 10 %
Daniel@0 11 % Returns:
Daniel@0 12 %
Daniel@0 13 % a - an m x 1 assignment vector, which gives the
Daniel@0 14 % minimum cost assignment. a(i) is the index of
Daniel@0 15 % the item of 1:n that was matched to item i of
Daniel@0 16 % 1:m. If item i (of 1:m) was not matched to any
Daniel@0 17 % item of 1:n, then a(i) is zero.
Daniel@0 18
Daniel@0 19 % Copyright (C) 2002 Mark A. Paskin
Daniel@0 20 %
Daniel@0 21 % This program is free software; you can redistribute it and/or modify
Daniel@0 22 % it under the terms of the GNU General Public License as published by
Daniel@0 23 % the Free Software Foundation; either version 2 of the License, or
Daniel@0 24 % (at your option) any later version.
Daniel@0 25 %
Daniel@0 26 % This program is distributed in the hope that it will be useful, but
Daniel@0 27 % WITHOUT ANY WARRANTY; without even the implied warranty of
Daniel@0 28 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Daniel@0 29 % General Public License for more details.
Daniel@0 30 %
Daniel@0 31 % You should have received a copy of the GNU General Public License
Daniel@0 32 % along with this program; if not, write to the Free Software
Daniel@0 33 % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
Daniel@0 34 % USA.
Daniel@0 35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 36
Daniel@0 37 function [a] = optimalMatching(C)
Daniel@0 38
Daniel@0 39 % Trivial cases:
Daniel@0 40 [p, q] = size(C);
Daniel@0 41 if (p == 0)
Daniel@0 42 a = [];
Daniel@0 43 return;
Daniel@0 44 elseif (q == 0)
Daniel@0 45 a = zeros(p, 1);
Daniel@0 46 return;
Daniel@0 47 end
Daniel@0 48
Daniel@0 49
Daniel@0 50 if 0
Daniel@0 51 % First, reduce the problem by making easy optimal matches. If two
Daniel@0 52 % elements agree that they are the best match, then match them up.
Daniel@0 53 [x, a] = min(C, [], 2);
Daniel@0 54 [y, b] = min(C, [], 1);
Daniel@0 55 u = find(1:p ~= b(a(:)));
Daniel@0 56 a(u) = 0;
Daniel@0 57 v = find(1:q ~= a(b(:))');
Daniel@0 58 C = C(u, v);
Daniel@0 59 if (isempty(C)) return; end
Daniel@0 60 end
Daniel@0 61
Daniel@0 62 % Get the (new) size of the two sets, u and v.
Daniel@0 63 [m, n] = size(C);
Daniel@0 64
Daniel@0 65 %mx = realmax;
Daniel@0 66 mx = 2*max(C(:));
Daniel@0 67 mn = -2*min(C(:));
Daniel@0 68 % Pad the affinity matrix to be square
Daniel@0 69 if (m < n)
Daniel@0 70 C = [C; mx * ones(n - m, n)];
Daniel@0 71 elseif (n < m)
Daniel@0 72 C = [C, mx * ones(m, m - n)];
Daniel@0 73 end
Daniel@0 74
Daniel@0 75 % Run the Hungarian method. First replace infinite values by the
Daniel@0 76 % largest (or smallest) finite values.
Daniel@0 77 C(find(isinf(C) & (C > 0))) = mx;
Daniel@0 78 C(find(isinf(C) & (C < 0))) = mn;
Daniel@0 79 %fprintf('running hungarian\n');
Daniel@0 80 [b, cost] = hungarian(C');
Daniel@0 81
Daniel@0 82 % Extract only the real assignments
Daniel@0 83 ap = b(1:m)';
Daniel@0 84 ap(find(ap > n)) = 0;
Daniel@0 85
Daniel@0 86 a = ap;
Daniel@0 87 %% Incorporate this sub-assignment into the complete assignment
Daniel@0 88 % k = find(ap);
Daniel@0 89 % a(u(k)) = v(ap(k));
Daniel@0 90