Jam*_*mes 4 matlab matrix pdist
我使用pdist命令查找存储在矩阵中的x和y坐标之间的距离.
X = [100 100;
0 100;
100 0;
500 400;
300 600;];
D = pdist(X,'euclidean')
Run Code Online (Sandbox Code Playgroud)
返回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]
Run Code Online (Sandbox Code Playgroud)
有没有办法将这些距离与它们的坐标相关联,即将它们存储在具有一般行形式的矩阵中:
[Length xcoordinate1 ycoordinate1 xcoordinate2 ycoordinate2]
Run Code Online (Sandbox Code Playgroud)
找到每个长度的行?
提前致谢
har*_*mug 12
MATLAB有一个名为"squareform"的内置命令,可将pdist输出转换为nxn距离矩阵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'));
Run Code Online (Sandbox Code Playgroud)
%# 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,:)];
Run Code Online (Sandbox Code Playgroud)