matthiasm@8: % Consider matching sources to detections matthiasm@8: matthiasm@8: % s1 d2 matthiasm@8: % s2 d3 matthiasm@8: % d1 matthiasm@8: matthiasm@8: %a = bipartiteMatchingHungarian([52;0.01]) matthiasm@8: matthiasm@8: % sources(:,i) = [x y] coords matthiasm@8: sources = [0.1 0.7; 0.6 0.4]'; matthiasm@8: detections = [0.2 0.2; 0.2 0.8; 0.7 0.1]'; matthiasm@8: dst = sqdist(sources, detections); matthiasm@8: matthiasm@8: % a = [2 3] which means s1-d2, s2-d3 matthiasm@8: a = bipartiteMatchingHungarian(dst); matthiasm@8: a2 = bipartiteMatchingIntProg(dst); matthiasm@8: assert(isequal(a(:),a2(:))) matthiasm@8: matthiasm@8: matthiasm@8: figure(1); clf matthiasm@8: bipartiteMatchingDemoPlot(sources, detections, a) matthiasm@8: matthiasm@8: matthiasm@8: matthiasm@8: matthiasm@8: %%%% Flip roles of sources and detections matthiasm@8: matthiasm@8: %dst = dst'; matthiasm@8: dst = sqdist(detections, sources); matthiasm@8: % a = [0 1 2] which means d1-0, d2-s1, d3-s2 matthiasm@8: a = bipartiteMatchingHungarian(dst); matthiasm@8: matthiasm@8: a2 = bipartiteMatchingIntProg(dst); matthiasm@8: assert(isequal(a(:),a2(:))) matthiasm@8: matthiasm@8: figure(2); clf matthiasm@8: bipartiteMatchingDemoPlot(detections, sources, a) % swapped args matthiasm@8: matthiasm@8: matthiasm@8: matthiasm@8: matthiasm@8: %%%%%%%%%% Move s1 nearer to d1 matthiasm@8: % d2 matthiasm@8: % s2 d3 matthiasm@8: % s1 d1 matthiasm@8: matthiasm@8: sources = [0.1 0.3; 0.6 0.4]'; matthiasm@8: detections = [0.2 0.2; 0.2 0.8; 0.7 0.1]'; matthiasm@8: dst = sqdist(sources, detections); matthiasm@8: matthiasm@8: % a = [2 3] which means s1-d2, s2-d3 matthiasm@8: a = bipartiteMatchingHungarian(dst); matthiasm@8: [a2, ass] = bipartiteMatchingIntProg(dst); matthiasm@8: assert(isequal(a(:),a2(:))) matthiasm@8: matthiasm@8: matthiasm@8: figure(3); clf matthiasm@8: bipartiteMatchingDemoPlot(sources, detections, a) matthiasm@8: matthiasm@8: matthiasm@8: matthiasm@8: %%%%%%%%%% matthiasm@8: matthiasm@8: % Use random points matthiasm@8: matthiasm@8: % Generate 2D data from a mixture of 2 Gaussians (from netlab demgmm1) matthiasm@8: randn('state', 0); rand('state', 0); matthiasm@8: gmix = gmm(2, 2, 'spherical'); matthiasm@8: ndat1 = 10; ndat2 = 10; ndata = ndat1+ndat2; matthiasm@8: %gmix.centres = [0.3 0.3; 0.7 0.7]; matthiasm@8: %gmix.covars = [0.01 0.01]; matthiasm@8: gmix.centres = [0.5 0.5; 0.5 0.5]; matthiasm@8: gmix.covars = [0.1 0.01]; matthiasm@8: [x, label] = gmmsamp(gmix, ndata); matthiasm@8: matthiasm@8: ndx = find(label==1); matthiasm@8: sources = x(ndx,:)'; matthiasm@8: ndx = find(label==2); matthiasm@8: detections = x(ndx,:)'; matthiasm@8: dst = sqdist(sources, detections); matthiasm@8: matthiasm@8: [a, ass] = bipartiteMatchingIntProg(dst); matthiasm@8: [a2] = bipartiteMatchingHungarian(dst); matthiasm@8: assert(isequal(a(:), a2(:))) matthiasm@8: matthiasm@8: figure(4); clf matthiasm@8: bipartiteMatchingDemoPlot(sources, detections, a) matthiasm@8: matthiasm@8: % only match 80% of points matthiasm@8: p1 = size(sources, 2); matthiasm@8: p2 = size(detections, 2); matthiasm@8: nmatch = ceil(0.8*min(p1,p2)); matthiasm@8: a2 = bipartiteMatchingIntProg(dst, nmatch); matthiasm@8: figure(5); clf matthiasm@8: bipartiteMatchingDemoPlot(sources, detections, a2) matthiasm@8: matthiasm@8: matthiasm@8: %%% swap roles matthiasm@8: matthiasm@8: ndx = find(label==2); matthiasm@8: sources = x(ndx,:)'; matthiasm@8: ndx = find(label==1); matthiasm@8: detections = x(ndx,:)'; matthiasm@8: dst = sqdist(sources, detections); matthiasm@8: matthiasm@8: % only match 80% of points matthiasm@8: p1 = size(sources, 2); matthiasm@8: p2 = size(detections, 2); matthiasm@8: nmatch = ceil(0.8*min(p1,p2)); matthiasm@8: a2 = bipartiteMatchingIntProg(dst, nmatch); matthiasm@8: figure(6); clf matthiasm@8: bipartiteMatchingDemoPlot(sources, detections, a2)