Finding closest point to known coordinates

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

 Accepted Answer

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 Comments

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
As always, my pleasure.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!