【问题标题】:MATLAB pdist functionMATLAB pdist 函数
【发布时间】:2011-01-29 10:26:57
【问题描述】:

我正在使用 pdist 命令来查找存储在矩阵中的 x 和 y 坐标之间的距离。

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

D = pdist(X,'euclidean')

它返回一个 15 元素向量。 :

[0.734979755525412 3.40039811339820 2.93175207511321   1.83879677592575 2.40127440268306 2.75251513299386 2.21488402640753 1.10610649500317 1.81674017301699 0.903207751535635 1.99116952754924 1.05069952386082 1.24122819418333 1.08583377275532 1.38729428638035]

有没有办法将这些距离与它们所衍生的坐标相关联,即将它们存储在具有一般行形式的矩阵中:

[Length xcoordinate1 ycoordinate1 xcoordinate2 ycoordinate2]

在哪里找到每个长度的一行?

提前致谢

【问题讨论】:

标签: matlab matrix pdist


【解决方案1】:
%# 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,:)];

【讨论】:

    【解决方案2】:

    您可以使用函数NCHOOSEKX 生成一组索引,并按以下方式构建您的矩阵:

    >> 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
    

    请注意,如果X 中的列数少于 15 左右,函数 NCHOOSEK 将是一个实用的解决方案。

    编辑:因为pdist 选择点对,所以nchoosek 的秒参数应该是2。它与数据的维度无关。这也使前一行的注释过时了。 (很抱歉以这种方式进行编辑,没有足够的代表添加评论,但我真的很喜欢这个答案并想修复它)——保罗

    【讨论】:

      【解决方案3】:

      MATLAB 有一个名为“squareform”的内置命令,可将 pdist 输出转换为 n x n 距离矩阵 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'));
      

      【讨论】:

      • 只是对遇到同样问题的python用户的评论。在 scipy 中,还可以使用squareformpdist 的结果转换成方阵。这些函数可以在scipy.spatial.distance 中找到。参看。 this post
      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 2012-03-18
      • 2022-11-15
      • 2012-03-27
      • 2015-03-01
      • 2014-03-23
      • 1970-01-01
      • 2014-11-11
      相关资源
      最近更新 更多