Skip to content

Commit cfdbe6d

Browse files
committed
matlab code
0 parents  commit cfdbe6d

File tree

12,811 files changed

+10859
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

12,811 files changed

+10859
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
function MIhat = MutualInfo(L1,L2)
2+
% mutual information
3+
%===========
4+
L1 = L1(:);
5+
L2 = L2(:);
6+
if size(L1) ~= size(L2)
7+
error('size(L1) must == size(L2)');
8+
end
9+
10+
Label = unique(L1);
11+
nClass = length(Label);
12+
13+
Label2 = unique(L2);
14+
nClass2 = length(Label2);
15+
if nClass2 < nClass
16+
% smooth
17+
L1 = [L1; Label];
18+
L2 = [L2; Label];
19+
elseif nClass2 > nClass
20+
% smooth
21+
L1 = [L1; Label2];
22+
L2 = [L2; Label2];
23+
end
24+
25+
26+
G = zeros(nClass);
27+
for i=1:nClass
28+
for j=1:nClass
29+
G(i,j) = sum(L1 == Label(i) & L2 == Label(j));
30+
end
31+
end
32+
sumG = sum(G(:));
33+
34+
P1 = sum(G,2); P1 = P1/sumG;
35+
P2 = sum(G,1); P2 = P2/sumG;
36+
if sum(P1==0) > 0 || sum(P2==0) > 0
37+
% smooth
38+
error('Smooth fail!');
39+
else
40+
H1 = sum(-P1.*log2(P1));
41+
H2 = sum(-P2.*log2(P2));
42+
P12 = G/sumG;
43+
PPP = P12./repmat(P2,nClass,1)./repmat(P1,1,nClass);
44+
PPP(abs(PPP) < 1e-12) = 1;
45+
MI = sum(P12(:) .* log2(PPP(:)));
46+
MIhat = MI / max(H1,H2);
47+
%%%%%%%%%%%%% why complex ? %%%%%%%%
48+
MIhat = real(MIhat);
49+
end
50+
51+
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
function [newL2] = bestMap(L1,L2)
2+
%bestmap: permute labels of L2 match L1 as good as possible
3+
% [newL2] = bestMap(L1,L2);
4+
5+
%===========
6+
L1 = L1(:);
7+
L2 = L2(:);
8+
if size(L1) ~= size(L2)
9+
error('size(L1) must == size(L2)');
10+
end
11+
12+
Label1 = unique(L1);
13+
nClass1 = length(Label1);
14+
Label2 = unique(L2);
15+
nClass2 = length(Label2);
16+
17+
nClass = max(nClass1,nClass2);
18+
G = zeros(nClass);
19+
for i=1:nClass1
20+
for j=1:nClass2
21+
G(i,j) = length(find(L1 == Label1(i) & L2 == Label2(j)));
22+
end
23+
end
24+
[c,t] = hungarian(-G);
25+
newL2 = zeros(size(L2));
26+
for i=1:nClass2
27+
newL2(L2 == Label2(i)) = Label1(c(i));
28+
end
29+
30+
31+
return;
32+
33+
%=======backup old===========
34+
35+
L1 = L1 - min(L1) + 1; % min (L1) <- 1;
36+
L2 = L2 - min(L2) + 1; % min (L2) <- 1;
37+
%=========== make bipartition graph ============
38+
nClass = max(max(L1), max(L2));
39+
G = zeros(nClass);
40+
for i=1:nClass
41+
for j=1:nClass
42+
G(i,j) = length(find(L1 == i & L2 == j));
43+
end
44+
end
45+
%=========== assign with hungarian method ======
46+
[c,t] = hungarian(-G);
47+
newL2 = zeros(nClass,1);
48+
for i=1:nClass
49+
newL2(L2 == i) = c(i);
50+
end

0 commit comments

Comments
 (0)