MATLAB:成对差异矩阵 [英] MATLAB: Matrix of pairwise differences
本文介绍了MATLAB:成对差异矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有值的NX1向量。我希望做的是创建一个N×N的矩阵,其中重$ P $每个值psents第i个第j值之间的差异 - 有点像一个大的相关性矩阵。我已经做了这一个循环,但我在寻找一个更优雅的方式使用MATLAB的矢量能力的方法,因为这种载体可能会相当大。
I have a Nx1 vector of values. What I would like to do is create a NxN matrix where each value represents the difference between the ith and jth value - sort of like a large correlation matrix. I've done with this with a loop but I'm looking for a more elegant way to approach using MATLAB's vectorization capabilities as this vector may get quite large.
推荐答案
怎么样
diff__ = bsxfun(@minus,repmat(A,N,1),A');
可以是的有了一定的提高做
diff__ = bsxfun(@minus,A,A');
一个小性能检查:
N = 1000;
v = rand(N,1);
tic
diff__ = bsxfun(@minus,repmat(v,N,1),v');
toc
tic
diff__ = bsxfun(@minus,v,v');
toc
结果
Elapsed time is 105.343344 seconds.
Elapsed time is 1.124946 seconds.
(Tim的数据检查:
(Tim's data check:
diff__ =
0 2 6 4
-2 0 4 2
-6 -4 0 -2
-4 -2 2 0
)。
这篇关于MATLAB:成对差异矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文