# How to measure the distance between 2 sets of points.

72 ビュー (過去 30 日間)
Jim Lehane 2013 年 3 月 4 日
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 件のコメント

Matt J 2013 年 3 月 4 日
Clarify what you mean by "is in a 1-to-1 ratio between X and Y".
Jim Lehane 2013 年 3 月 4 日
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...)

サインイン to comment.

### 採用された回答

Matt J 2013 年 3 月 4 日

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 件のコメント

Matt J 2013 年 3 月 4 日
[result,where] = min(sum(DistMatrix(idx)));
DistMatrix(idx(:,where))
Jim Lehane 2013 年 3 月 4 日
You make it seem so simple :-). Thanks again.
udara darshana panamulle arachchige 2018 年 12 月 10 日
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

サインイン to comment.

Azzi Abdelmalek 2013 年 3 月 4 日

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 件のコメント

Jim Lehane 2013 年 3 月 4 日
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.
Jim Lehane 2013 年 3 月 4 日
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!).
Matt J 2013 年 3 月 4 日