Hi,
I want to find a coordinate which has minimum distance to other coordinates,
For example;
I want to find a coordinate (a,b) which has minimum distance to coordinates given below
x y
1 2
1 4
3 2
4 2

 채택된 답변

Star Strider
Star Strider 2019년 3월 20일

0 개 추천

The minimum distance of a set of points is the Centroid (link), defined as the mean of the coordinates of the points:
xy = [1 2
1 4
3 2
4 2];
minDist = mean(xy);
figure
plot(xy(:,1), xy(:,2), 'pg')
hold on
plot(minDist(1), minDist(2), 'rp')
hold off
grid
axis equal
Experiment toi get the result you want.

댓글 수: 6

Thank you for the answer, but i also need to find which coordinate (input coordinate) is closest to calculated mean coordinate
As always, my pleasure.
Use the pdist2 (link) function:
[dist,idx] = pdist2(xy, minDist, 'euclidean', 'Smallest',1)
producing here:
dist =
0.90139
idx =
3
that being row 3: (3,2).
Thank you very much for the answer, I am sorry perhaps it is very easy but i am too new with matlab.
But how can i assign the coordinates in row 3 to a variable?
I mean i want to assign the closest point to a variable.
Thank you for your support again
As always, my pleasure.
Use the ‘idx’ value:
newVar = xy(idx,:)
producing:
newVar =
3 2
Thank you again
Sincerely
Star Strider
Star Strider 2019년 3월 21일
As always, my pleasure.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by