计算向量中的所有差异可能性 [英] Compute all differences possibilities in a vector
本文介绍了计算向量中的所有差异可能性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个简短的向量x = [a,b,c,d,e];
,计算向量成员之间所有差的最佳方法是什么?
Let's say I have a short vector x = [a,b,c,d,e];
What would be the best way to compute all the difference between members of the vector as:
y = [e-d e-c e-b e-a
d-e d-c d-b d-a
c-e c-d c-b c-a
b-e b-d b-c b-a
a-e a-d a-c a-b];
预先感谢
推荐答案
要给出确切的矩阵,请尝试:
To give that exact matrix, try:
x = [1;2;3;4;5]; %# note this is a column vector (matrix of rows in general)
D = squareform( pdist(x,@(p,q)q-p) );
U = triu(D);
L = tril(D);
y = flipud(fliplr( L(:,1:end-1) - U(:,2:end) ))
在这种情况下的结果:
y =
1 2 3 4
-1 1 2 3
-2 -1 1 2
-3 -2 -1 1
-4 -3 -2 -1
这篇关于计算向量中的所有差异可能性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文