-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbestMap.m
executable file
·50 lines (43 loc) · 1.06 KB
/
bestMap.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function [newL2] = bestMap(L1,L2)
%bestmap: permute labels of L2 match L1 as good as possible
% [newL2] = bestMap(L1,L2);
%===========
L1 = L1(:);
L2 = L2(:);
if size(L1) ~= size(L2)
error('size(L1) must == size(L2)');
end
Label1 = unique(L1);
nClass1 = length(Label1);
Label2 = unique(L2);
nClass2 = length(Label2);
nClass = max(nClass1,nClass2);
G = zeros(nClass);
for i=1:nClass1
for j=1:nClass2
G(i,j) = length(find(L1 == Label1(i) & L2 == Label2(j)));
end
end
[c,t] = hungarian(-G);
newL2 = zeros(size(L2));
for i=1:nClass2
newL2(L2 == Label2(i)) = Label1(c(i));
end
return;
%=======backup old===========
L1 = L1 - min(L1) + 1; % min (L1) <- 1;
L2 = L2 - min(L2) + 1; % min (L2) <- 1;
%=========== make bipartition graph ============
nClass = max(max(L1), max(L2));
G = zeros(nClass);
for i=1:nClass
for j=1:nClass
G(i,j) = length(find(L1 == i & L2 == j));
end
end
%=========== assign with hungarian method ======
[c,t] = hungarian(-G);
newL2 = zeros(nClass,1);
for i=1:nClass
newL2(L2 == i) = c(i);
end