MATLAB: Find closest Coordinates to a point

mathematicsmatrixvector

I need to find closest point to A=[6,8] among B=[1,2 ; 5,7 ; 3,10 ; …], and i need to return those coordinates: for example in this case: [5,7]

Best Answer

In your example, you are returning A, rather than the closest point in B... assuming that the answer you are looking for was actually [5,7], then the following should get the job done:
%make some example random values:
A = rand(1,2);
B = rand(10,2);
%compute Euclidean distances:
distances = sqrt(sum(bsxfun(@minus, B, A).^2,2));
%find the smallest distance and use that as an index into B:
closest = B(find(distances==min(distances)),:);
Should work fine!