Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MATLAB pdist函数_Matlab_Matrix_Pdist - Fatal编程技术网

MATLAB pdist函数

MATLAB pdist函数,matlab,matrix,pdist,Matlab,Matrix,Pdist,我正在使用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.

我正在使用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]
找到的每种长度都有一行

提前感谢

您可以使用该函数将一组索引生成到
X
中,并按以下方式构建矩阵:

%# 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,:)];
>> 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左右时,该函数才是一个实用的解决方案


edit:由于
pdist
选择成对的点,因此
nchoosek
的秒参数应该是
2
。它与数据的维度无关。这也使得前一行的注释过时。(很抱歉这样编辑,没有足够的代表添加注释,但我真的很喜欢这个答案,并想修复它)——Paul

MATLAB有一个名为“squareform”的内置命令,可以将pdist输出转换为nxn距离矩阵


你的例子正确吗?我得到了一个10个元素的向量,它的值非常不同。您可能会发现另一个有用的函数是SQUAREFORM,它只是一个针对遇到相同问题的python用户的注释。在scipy中,还可以使用
squareform
pdist
的结果转换为方形数组。这些函数可以在
scipy.space.distance
中找到。查阅
%# define X, D
X = [100 100;
      0  100;
      100  0;
     500 400;
     300 600;];

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