Getting the minimum distance in a coordinates array

11 Ansichten (letzte 30 Tage)
Sabato De Gregorio
Sabato De Gregorio am 9 Jun. 2018
Beantwortet: Paolo am 9 Jun. 2018
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?

Antworten (1)

Paolo
Paolo am 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

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by