annotate _FullBNT/KPMtools/bipartiteMatchingDemo.m @ 9:4ea6619cb3f5 tip

removed log files
author matthiasm
date Fri, 11 Apr 2014 15:55:11 +0100
parents b5b38998ef3b
children
rev   line source
matthiasm@8 1 % Consider matching sources to detections
matthiasm@8 2
matthiasm@8 3 % s1 d2
matthiasm@8 4 % s2 d3
matthiasm@8 5 % d1
matthiasm@8 6
matthiasm@8 7 %a = bipartiteMatchingHungarian([52;0.01])
matthiasm@8 8
matthiasm@8 9 % sources(:,i) = [x y] coords
matthiasm@8 10 sources = [0.1 0.7; 0.6 0.4]';
matthiasm@8 11 detections = [0.2 0.2; 0.2 0.8; 0.7 0.1]';
matthiasm@8 12 dst = sqdist(sources, detections);
matthiasm@8 13
matthiasm@8 14 % a = [2 3] which means s1-d2, s2-d3
matthiasm@8 15 a = bipartiteMatchingHungarian(dst);
matthiasm@8 16 a2 = bipartiteMatchingIntProg(dst);
matthiasm@8 17 assert(isequal(a(:),a2(:)))
matthiasm@8 18
matthiasm@8 19
matthiasm@8 20 figure(1); clf
matthiasm@8 21 bipartiteMatchingDemoPlot(sources, detections, a)
matthiasm@8 22
matthiasm@8 23
matthiasm@8 24
matthiasm@8 25
matthiasm@8 26 %%%% Flip roles of sources and detections
matthiasm@8 27
matthiasm@8 28 %dst = dst';
matthiasm@8 29 dst = sqdist(detections, sources);
matthiasm@8 30 % a = [0 1 2] which means d1-0, d2-s1, d3-s2
matthiasm@8 31 a = bipartiteMatchingHungarian(dst);
matthiasm@8 32
matthiasm@8 33 a2 = bipartiteMatchingIntProg(dst);
matthiasm@8 34 assert(isequal(a(:),a2(:)))
matthiasm@8 35
matthiasm@8 36 figure(2); clf
matthiasm@8 37 bipartiteMatchingDemoPlot(detections, sources, a) % swapped args
matthiasm@8 38
matthiasm@8 39
matthiasm@8 40
matthiasm@8 41
matthiasm@8 42 %%%%%%%%%% Move s1 nearer to d1
matthiasm@8 43 % d2
matthiasm@8 44 % s2 d3
matthiasm@8 45 % s1 d1
matthiasm@8 46
matthiasm@8 47 sources = [0.1 0.3; 0.6 0.4]';
matthiasm@8 48 detections = [0.2 0.2; 0.2 0.8; 0.7 0.1]';
matthiasm@8 49 dst = sqdist(sources, detections);
matthiasm@8 50
matthiasm@8 51 % a = [2 3] which means s1-d2, s2-d3
matthiasm@8 52 a = bipartiteMatchingHungarian(dst);
matthiasm@8 53 [a2, ass] = bipartiteMatchingIntProg(dst);
matthiasm@8 54 assert(isequal(a(:),a2(:)))
matthiasm@8 55
matthiasm@8 56
matthiasm@8 57 figure(3); clf
matthiasm@8 58 bipartiteMatchingDemoPlot(sources, detections, a)
matthiasm@8 59
matthiasm@8 60
matthiasm@8 61
matthiasm@8 62 %%%%%%%%%%
matthiasm@8 63
matthiasm@8 64 % Use random points
matthiasm@8 65
matthiasm@8 66 % Generate 2D data from a mixture of 2 Gaussians (from netlab demgmm1)
matthiasm@8 67 randn('state', 0); rand('state', 0);
matthiasm@8 68 gmix = gmm(2, 2, 'spherical');
matthiasm@8 69 ndat1 = 10; ndat2 = 10; ndata = ndat1+ndat2;
matthiasm@8 70 %gmix.centres = [0.3 0.3; 0.7 0.7];
matthiasm@8 71 %gmix.covars = [0.01 0.01];
matthiasm@8 72 gmix.centres = [0.5 0.5; 0.5 0.5];
matthiasm@8 73 gmix.covars = [0.1 0.01];
matthiasm@8 74 [x, label] = gmmsamp(gmix, ndata);
matthiasm@8 75
matthiasm@8 76 ndx = find(label==1);
matthiasm@8 77 sources = x(ndx,:)';
matthiasm@8 78 ndx = find(label==2);
matthiasm@8 79 detections = x(ndx,:)';
matthiasm@8 80 dst = sqdist(sources, detections);
matthiasm@8 81
matthiasm@8 82 [a, ass] = bipartiteMatchingIntProg(dst);
matthiasm@8 83 [a2] = bipartiteMatchingHungarian(dst);
matthiasm@8 84 assert(isequal(a(:), a2(:)))
matthiasm@8 85
matthiasm@8 86 figure(4); clf
matthiasm@8 87 bipartiteMatchingDemoPlot(sources, detections, a)
matthiasm@8 88
matthiasm@8 89 % only match 80% of points
matthiasm@8 90 p1 = size(sources, 2);
matthiasm@8 91 p2 = size(detections, 2);
matthiasm@8 92 nmatch = ceil(0.8*min(p1,p2));
matthiasm@8 93 a2 = bipartiteMatchingIntProg(dst, nmatch);
matthiasm@8 94 figure(5); clf
matthiasm@8 95 bipartiteMatchingDemoPlot(sources, detections, a2)
matthiasm@8 96
matthiasm@8 97
matthiasm@8 98 %%% swap roles
matthiasm@8 99
matthiasm@8 100 ndx = find(label==2);
matthiasm@8 101 sources = x(ndx,:)';
matthiasm@8 102 ndx = find(label==1);
matthiasm@8 103 detections = x(ndx,:)';
matthiasm@8 104 dst = sqdist(sources, detections);
matthiasm@8 105
matthiasm@8 106 % only match 80% of points
matthiasm@8 107 p1 = size(sources, 2);
matthiasm@8 108 p2 = size(detections, 2);
matthiasm@8 109 nmatch = ceil(0.8*min(p1,p2));
matthiasm@8 110 a2 = bipartiteMatchingIntProg(dst, nmatch);
matthiasm@8 111 figure(6); clf
matthiasm@8 112 bipartiteMatchingDemoPlot(sources, detections, a2)