Getting the minimum distance in a coordinates array

조회 수: 11 (최근 30일)
Sabato De Gregorio
Sabato De Gregorio 2018년 6월 9일
답변: Paolo 2018년 6월 9일
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