How to measure the distance between 2 sets of points.
Show older comments
I was wondering how I measure the distance between 2 sets of coordinates.
I have 2 matrices where each matrix (X and Y) have the same number of rows (m x 2). I calculated out the distance from every point in X to every point in Y using the pdist2 tool. This gave me a m x m matrix of distances.
I want the minimum distance between X and Y where each coordinate is in a 1-to-1 ratio between X and Y. I am not sure how to separate the distances out of the pdist2 tool to discrete sets.
Does anyone know how to do this or where I can find the answer?
2 Comments
Matt J
on 4 Mar 2013
Clarify what you mean by "is in a 1-to-1 ratio between X and Y".
Jim Lehane
on 4 Mar 2013
Edited: Jim Lehane
on 4 Mar 2013
Accepted Answer
More Answers (1)
Azzi Abdelmalek
on 4 Mar 2013
Edited: Azzi Abdelmalek
on 4 Mar 2013
M1 & M2 are your two matrices
M1=rand(10,2);
M2=rand(10,2)
dist=sqrt((M2(:,1)-M1(:,1)).^2+(M2(:,2)-M1(:,2)).^2)
min_dist=min(dist)
13 Comments
Jim Lehane
on 4 Mar 2013
Azzi Abdelmalek
on 4 Mar 2013
You can do:
M1=rand(10,2);
M2=rand(10,2)
for k=1:size(M1,1)
dist(k)=min(sqrt((M2(k,1)-M1(:,1)).^2+(M2(k,2)-M1(:,2)).^2))
end
min_dist=min(dist)
Jim Lehane
on 4 Mar 2013
Jim Lehane
on 4 Mar 2013
Azzi Abdelmalek
on 4 Mar 2013
Edited: Azzi Abdelmalek
on 4 Mar 2013
Check this, I did a little error
M1=rand(10,2);
M2=rand(10,2)
for k=1:size(M1,1)
[val,idx]=min(sqrt((M2(:,1)-M1(k,1)).^2+(M2(:,2)-M1(k,2)).^2))
dist(k)=val;
indice(k)=idx
end
[min_dist,idx_min]=min(dist) % min_dist is the min value
M1_c=M1(idx_min,:) % Point M1
M2_c=M2(indice(idx_min),:) % Point M2
Azzi Abdelmalek
on 4 Mar 2013
Edited: Azzi Abdelmalek
on 4 Mar 2013
The above code calculate the distance between each point of M1 with all points of M2. And gives the coordinates of the two points corresponding to minimal distance
Jim Lehane
on 4 Mar 2013
Azzi Abdelmalek
on 4 Mar 2013
Edited: Azzi Abdelmalek
on 4 Mar 2013
You have to be clear, are you looking for distance between points from X and points from Y or do you want to create one array Z=unique([X;Y],'rows') and look for minimal distance between the points of Z
Jim Lehane
on 4 Mar 2013
You are using the word "ratio" either vaguely or incorrectly and it is causing great confusion for us. A ratio is a fraction expressing a proportion, like 3/2 or 5/7.
If you mean "1-1 mapping", then obviously there is a 1-1 mapping between X and Y because they both contain m points, but that mapping is not unique. What mapping are you thinking of? For a given X(i,:) how does one determine the corresponding Y(j,:).
Jim Lehane
on 4 Mar 2013
Edited: Jim Lehane
on 4 Mar 2013
Jim Lehane
on 4 Mar 2013
Matt J
on 4 Mar 2013
See my Answer.
Categories
Find more on Hierarchical Clustering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!