How to measure the distance between 2 sets of points.

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 commentaires

Clarify what you mean by "is in a 1-to-1 ratio between X and Y".
Jim Lehane
Jim Lehane le 4 Mar 2013
Modifié(e) : Jim Lehane le 4 Mar 2013
Every point in X corresponds with 1 point in Y. So that there are no duplicates. (ie X1 to Y3; X2 to Y1; X3 to Y2...)

Connectez-vous pour commenter.

 Réponse acceptée

Matt J
Matt J le 4 Mar 2013
Modifié(e) : Matt J le 4 Mar 2013
m=3;
Q=perms(1:m).';
P=repmat((1:m).',1,size(Q,2));
idx=sub2ind([m,m],P,Q);
result = min(sum(DistMatrix(idx)))

5 commentaires

Jim Lehane
Jim Lehane le 4 Mar 2013
Modifié(e) : Jim Lehane le 4 Mar 2013
YES!!!!! YES!!! YES!!!!
Thank you so much. I knew there had to be a fairly straight forward method.
Is there a way to get the individual distances that add up to the minimum distance that your equation creates? (i.e. in this instance the 3 distances that add up to the 'result').
Matt J
Matt J le 4 Mar 2013
Modifié(e) : Matt J le 4 Mar 2013
[result,where] = min(sum(DistMatrix(idx)));
DistMatrix(idx(:,where))
You make it seem so simple :-). Thanks again.
I have a same qustion but data set size is bit larger A(15,3) and B(105,3)
using this method gives a error due to exceeding maximum array size.
Is there any other way to do this Matt J??
Q=perms(1:15).'; %% here I get the error
P=repmat((1:103).',1,size(Q,2));
idx=sub2ind([15,103],P,Q);
result = min(sum(DistMatrix(idx)));
ps: my qustion is same with Jim Lehane

Connectez-vous pour commenter.

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 4 Mar 2013
Modifié(e) : Azzi Abdelmalek le 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 commentaires

Does this take into effect if the sets are not ordered. I don't want X1 to correspond Y1, X2 to Y2, X3 to Y3... I want it to check every combination of X to Y to see what is the shortest distance.
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)
Yea, after looking at yours again. You only measure the distances between each point and give me the minimum distance between those points. I want the minimum distance of each set. For a matrix of 10 points there should be 10! combination of sets. I want the minimum distance of those. Not just which 2 points are closest together.
Your second answer is still giving me the same thing. I figured I could get the smallest distance by adding together every distance between every point for each unique set of combinations. You still are only giving me the minimum distance between 2 individual points.
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
Azzi Abdelmalek le 4 Mar 2013
Modifié(e) : Azzi Abdelmalek le 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
I'm not sure exactly what the equation is outputting but when I put in my matrices it gives me zero as the minimum because two of the points over lap. These are a fraction of the matrices I have:
X = [10 4;7 5;8 5;9 5;10 5;11 5;12 5;13 5;6 6;5 7;15 7;4 8;15 8];
Y = [10 7;11 7;12 7;13 7;14 7;8 8;9 8;15 8;7 9;6 10;17 10;6 11;18 11];
I don't think that is the way to get what I want. Pdist2 seems the best method to work through but I'm not sure how to break down the matrix into individual unique sets.
Azzi Abdelmalek
Azzi Abdelmalek le 4 Mar 2013
Modifié(e) : Azzi Abdelmalek le 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
I am looking for the minimum combined distance from every point in X to every point in Y where there is a 1-to-1 ratio of points in X to points in Y.
Matt J
Matt J le 4 Mar 2013
Modifié(e) : Matt J le 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
Jim Lehane le 4 Mar 2013
Modifié(e) : Jim Lehane le 4 Mar 2013
I'm sorry, you are correct. I mean a 1-to-1 mapping of points. I want to measure the distance in every point in X to every point in Y where each point in X coresponds (maps) to one point in Y, regardless of the specific order the points initially started out in. This can be done in pdist2.
My problem is taking those distances out of the matrix and finding the smallest combined distance for each unique set of X to Y measurements.
I hope that is a little clearer.
For example, say I have 2 matrices X and Y with 3 coordinates each. The matrix below represents the pdist2 result of those with the distances.
Y1 Y2 Y3
X1 2 5 7
X2 4 1 6
X3 3 8 9
I want the minimum combined distance of each unique combination of distance:
X1,Y1 + X2,Y2 + X3,Y3 =
X1,Y1 + X2,Y3 + X3,Y2 =
X1,Y2 + X2,Y1 + X3,Y3 =
X1,Y2 + X2,Y3 + X3,Y1 =
X1,Y3 + X2,Y1 + X3,Y2 =
X1,Y3 + X2,Y3 + X3,Y1 =
In this instance there are a total of 6 possible combination of coordinates (3!).
See my Answer.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by