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

 Réponse acceptée

Star Strider
Star Strider le 20 Mar 2019
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 commentaires

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 le 21 Mar 2019
As always, my pleasure.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by