Getting the minimum distance in a coordinates array

1 次查看(过去 30 天)
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?

回答(1 个)

Paolo
Paolo 2018-6-9
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

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by