Getting the minimum distance in a coordinates array

3 views (last 30 days)
Let's say I have this array of coordinates (values are (x,y)):
centers =
938 845
810 1012
147 875
1162 810
494 1039
383 897
203 1010
I need to pick 2 coordinates with the lowest difference on the X-axis (first column) because I need 2 points that are as closest as possible to each other. How can I do it?

Answers (1)

Paolo
Paolo on 9 Jun 2018
Your input:
x = [938;810;147;1162;494;383;203];
y = [845;1012;875;810;1039;897;1010];
Sort x:
[tmpx,i] = sort(x);
%Order y according to sorting index for x.
tmpy = y(i);
Your two points are:
P1 = [tmpx(1),tmpy(1)]
P2 = [tmpx(2),tmpy(2)]
Output:
P1 = 147 875
P2 = 203 1010

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!