annotate toolboxes/FullBNT-1.0.7/KPMtools/bipartiteMatchingDemo.m @ 0:e9a9cd732c1e tip

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