Problem with Finding the Distance Between 2 Points

2 Ansichten (letzte 30 Tage)
Noushin Farnoud
Noushin Farnoud am 17 Nov. 2011
Hello Matlab Users,
I have a plot and would like to find the closest data point in this plot to my ginput() selection. But interestingly after plotting the data point with apparently the minimum distance, I noticed there are many other points on the plot that are clearly closer to the ginput selection! Here's an example of this problem. The Reference (m) (red star) can be considered as ginput selection. Point #3 (green box) is found to have the minimum distance to the reference. However, I think point #1 should be the closest one.
I'm not sure what I am doing wrong here, but would appreciate your help.
x=[0.3582 434.1973;
0.3522 424.8328;
0.3435 427.1739];
figure;
plot(x(:,1),x(:,2),'o'); hold on
text(x(:,1)+0.001,x(:,2),{'1';'2';'3'},'FontSize',12,'Color','k')
m =[0.3640 429.5151];
plot(m(1),m(2),'*r');
text(m(1)+0.001,m(2),{'Reference (m)'},'FontSize',12,'Color','r')
for i=1:3
d(i) = pdist([m;x(i,:)],'euclidean');
end
[mm,IX]=min(d);
plot(x(IX,1),x(IX,2),'s','MarkerSize',12,'MarkerEdgeColor','g')
Best Regards, Noushin

Antworten (1)

Andrew Newell
Andrew Newell am 17 Nov. 2011
Your calculation is correct, but the scales on your x and y axes are very different. The apparently large horizontal difference is actually much smaller than the vertical distances. Try typing axis equal to see it to scale.

Kategorien

Mehr zu Data Exploration finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by