MATLAB: Sorting points based on comparing distances.

comparing distancesindexingmatrix manipulationpdist2sorting points

Hello, I am using matlab 2019a and I am trying to sort points based on comparing the distance between them. I use the pdist2 function to calculate the the distance between the points stored in matricies. One matrix has 2 sets of coordinates
A= [0 0, -5.52181708330649 5.78874218621840]
the other has 7 sets
B = [38.8197976955836 -29.0434116907097
-37.0532684880158 0.644925865563445
-4.49735986053992 57.3402937422674
-43.7442096431328 38.5935144262550
41.5359946082739 41.4696332098067
57.3572679057450 8.87552592324304
-29.8320366435934 -43.1286701525403]
After calculating the distance between each point in A and every point in B i end up with a 2×7 matrix C. C(1,1) is the distance between the first point in A and the first point in B while C(2,1) is the distance between point 2 in A and point 1 in B. I want to compare each of these distances between C(1,2) to C(2,2)…. to the end and sort the points in B based on which point in A they are closest to. I hope to end up with a matrix of points closest to the first point in A and another matrix of points closest to the second point in A. If the code works with a variable number of points in A that would be greatly apprecitated. Thanks for the help.

Best Answer

Use the 2nd output to min() to pair the points in B to the nearest points in A. Then use splitapply to segment the points in B into nearest neighbors in A. This works for any amount of coordinates in A.
% Creat demo data
A= [0 0;
-5.52181708330649 5.78874218621840];
B = [38.8197976955836 -29.0434116907097
-37.0532684880158 0.644925865563445
-4.49735986053992 57.3402937422674
-43.7442096431328 38.5935144262550
41.5359946082739 41.4696332098067
57.3572679057450 8.87552592324304
-29.8320366435934 -43.1286701525403];
% compute distances between A, B
C = pdist2(A,B);
% or better: C = sqrt((B(:,1)-A(:,1)').^2 + (B(:,2)-A(:,2)').^2)';
% Split B into groups that are nearest to points in A
[~, minRowIdx] = min(C,[],1); %
neighborGroups = splitapply(@(x){x},B,minRowIdx(:));
neighborGroups{n} are all the coordinates in B that are closest to A(n,:).