# MATLAB: Comparing two sets of coordinates

coordinate comparisonMATLABparticle comparison

Hello
I have two different datasets from two particle scans of a silicon wafer. Each dataset contains a number of x- and y-coordinates from particles found on the wafer. I now want to compare the pre dataset with the post dataset, and see if there are two particles that lie very close (within 30 um) and therefore must be the same.
Right now I am basically comparing each of the points from the pre measurment with all the points from the post measurement and see if any match. However, each dataset can contain several thousands of coordinates, which tend to make the process somewhat slow. Any suggestions on how to make this faster and smarter?
Kind regards Martin

%  Make some datan1 = 48;n2 = 42;x1 = rand(n1,1);y1 = rand(n1,1);x2 = rand(n2,1);y2 = rand(n2,1);figureplot(x1,y1,'o',x2,y2,'x')% calculate all pairwise distancesticchanges = zeros(n1,n2);for k = 1:n2    changes(:,k) = (x1-x2(k)).^2 + (y1-y2(k)).^2;endtoc% find distances less than a given tolerancetol = 0.001;[j,k] = find(changes < tol);line(x1(j),y1(j),'color','r','marker','.','linestyle','none')line(x2(k),y2(k),'color','r','marker','.','linestyle','none')