优化MATLAB代码(嵌套循环以计算相似度矩阵) [英] Optimize MATLAB code (nested for loop to compute similarity matrix)
问题描述
for i=1:N % M,N is the size of the matrix x for whose elements I am computing similarity matrix
for j=1:N
D(i,j) = sqrt(sum(x(:,i)-x(:,j)).^2)); % D is the similarity matrix
end
end
任何优化方面的帮助吗?因为我的矩阵x
的尺寸为256x30000
,因此减少了for循环.
非常感谢!
-Aditya
在matlab中执行此功能的函数称为pdist.不幸的是,它非常缓慢,并且没有考虑Matlab的矢量化功能.
以下是我为项目编写的代码.让我知道您能获得什么样的速度.
Qx=repmat(dot(x,x,2),1,size(x,1));
D=sqrt(Qx+Qx'-2*x*x');
请注意,这仅在数据点位于行中且尺寸标注为列时才有效.例如,假设我有256个数据点和100000个维,然后在我的Mac上使用x = rand(256,100000),上面的代码在大约半秒钟内生成了256x256矩阵.
I am computing a similarity matrix based on Euclidean distance in MATLAB. My code is as follows:
for i=1:N % M,N is the size of the matrix x for whose elements I am computing similarity matrix
for j=1:N
D(i,j) = sqrt(sum(x(:,i)-x(:,j)).^2)); % D is the similarity matrix
end
end
Can any help with optimizing this = reducing the for loops as my matrix x
is of dimension 256x30000
.
Thanks a lot!
--Aditya
The function to do so in matlab is called pdist. Unfortunately it is painfully slow and doesnt take Matlabs vectorization abilities into account.
The following is code I wrote for a project. Let me know what kind of speed up you get.
Qx=repmat(dot(x,x,2),1,size(x,1));
D=sqrt(Qx+Qx'-2*x*x');
Note though that this will only work if your data points are in the rows and your dimensions the columns. So for example lets say I have 256 data points and 100000 dimensions then on my mac using x=rand(256,100000) and the above code produces a 256x256 matrix in about half a second.
这篇关于优化MATLAB代码(嵌套循环以计算相似度矩阵)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!