开发者

MATLAB pdist function

I am using the pdist command to find the distance between x and y coordinates stored in a matrix.

X = [100 100;
      0  100;
     100  0;
     500 400;
     300 600;];

D = pdist(X,'euclidean')

Which returns a 15 element vector. :

[0.734979755525412 3.40039811339820 2.93175207511321   1.83879677592575 2.40127440268306 2.7525151开发者_开发知识库3299386 2.21488402640753 1.10610649500317 1.81674017301699 0.903207751535635 1.99116952754924 1.05069952386082 1.24122819418333 1.08583377275532 1.38729428638035]

Is there a way to associate these distances with the coordinates they were derived from, i.e. store them in a matrix with the general row form:

[Length xcoordinate1 ycoordinate1 xcoordinate2 ycoordinate2]

Where there is a row for each length found?

Thanks in advance


MATLAB has an inbuilt command called "squareform" that converts a pdist output to an n x n distance matrix http://www.kxcad.net/cae_MATLAB/toolbox/stats/pdist.html

%# define X, D
X = [100 100;
      0  100;
      100  0;
     500 400;
     300 600;];

D = squareform(pdist(X,'euclidean'));


%# define X, D
X = [100 100;
      0  100;
     100  0;
     500 400;
     300 600;];

D = pdist(X,'euclidean');

%# find the indices corresponding to each distance
tmp = ones(size(X,1));
tmp = tril(tmp,-1); %# creates a matrix that has 1's below the diagonal

%# get the indices of the 1's
[rowIdx,colIdx ] = find(tmp);

%# create the output
out = [D',X(rowIdx,:),X(colIdx,:)];


You can use the function NCHOOSEK to generate a set of indices into X and build your matrix in the following way:

>> X = [100 100; 0 100; 100 0; 500 400; 300 600];  %# Your sample data
>> D = pdist(X,'euclidean')'  %'# Euclidean distance, with result transposed

D =

  100.0000    %# Note that I get different results than your example!
  100.0000
  500.0000
  538.5165
  141.4214
  583.0952
  583.0952
  565.6854
  632.4555
  282.8427

>> index = nchoosek(1:size(X,1),2);
>> M = [D X(index(:,1),:) X(index(:,2),:)]    %# [Distance X1 Y1 X2 Y2]

M =

  100.0000  100.0000  100.0000         0  100.0000
  100.0000  100.0000  100.0000  100.0000         0
  500.0000  100.0000  100.0000  500.0000  400.0000
  538.5165  100.0000  100.0000  300.0000  600.0000
  141.4214         0  100.0000  100.0000         0
  583.0952         0  100.0000  500.0000  400.0000
  583.0952         0  100.0000  300.0000  600.0000
  565.6854  100.0000         0  500.0000  400.0000
  632.4555  100.0000         0  300.0000  600.0000
  282.8427  500.0000  400.0000  300.0000  600.0000

Note that the function NCHOOSEK will only be a practical solution if the number of columns in X is less than around 15.

edit: since pdist selects pairs of points, the seconds argument to nchoosek should simply be 2. It is independent of the dimensionality of your data. This also makes the note on the preceding line obsolete. (sorry for the edit this way, not enough rep to add a comment, but I really liked this answer and wanted to fix it) -- Paul

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜