MATLAB版本7中的pdist2等效项 [英] pdist2 equivalent in MATLAB version 7
问题描述
我需要在matlab中计算2个矩阵之间的欧式距离.目前,我正在使用bsxfun并按如下方式计算距离(我正在附上代码片段):
I need to calculate the euclidean distance between 2 matrices in matlab. Currently I am using bsxfun and calculating the distance as below( i am attaching a snippet of the code ):
for i=1:4754
test_data=fea_test(i,:);
d=sqrt(sum(bsxfun(@minus, test_data, fea_train).^2, 2));
end
fea_test的大小是4754x1024,fea_train是6800x1024,使用他的for循环导致for的执行大约需要12分钟,我认为这太高了. 有没有一种方法可以更快地计算两个矩阵之间的欧式距离?
Size of fea_test is 4754x1024 and fea_train is 6800x1024 , using his for loop is causing the execution of the for to take approximately 12 minutes which I think is too high. Is there a way to calculate the euclidean distance between both the matrices faster?
有人告诉我,通过删除不必要的for循环,我可以减少执行时间.我也知道pdist2可以帮助减少计算时间,但是由于我使用的是matlab版本7,因此我没有pdist2函数.不能升级.
I was told that by removing unnecessary for loops I can reduce the execution time. I also know that pdist2 can help reduce the time for calculation but since I am using version 7. of matlab I do not have the pdist2 function. Upgrade is not an option.
任何帮助.
此致
Bhavya
推荐答案
您可以通过重复fea_test
6800次和fea_train
4754次的行来完全向量化计算,如下所示:
You could fully vectorize the calculation by repeating the rows of fea_test
6800 times, and of fea_train
4754 times, like this:
rA = size(fea_test,1);
rB = size(fea_train,1);
[I,J]=ndgrid(1:rA,1:rB);
d = zeros(rA,rB);
d(:) = sqrt(sum(fea_test(J(:),:)-fea_train(I(:),:)).^2,2));
但是,这将导致中间数组的大小为6800x4754x1024(* 8字节为双精度),这将占用约250GB的RAM.因此,完全矢量化将无法正常工作.
However, this would lead to intermediary arrays of size 6800x4754x1024 (*8 bytes for doubles), which will take up ~250GB of RAM. Thus, the full vectorization won't work.
但是,您可以通过预分配和在需要时不计算平方根来减少距离计算的时间:
You can, however, reduce the time of the distance calculation by preallocation, and by not calculating the square root before it's necessary:
rA = size(fea_test,1);
rB = size(fea_train,1);
d = zeros(rA,rB);
for i = 1:rA
test_data=fea_test(i,:);
d(i,:)=sum( (test_data(ones(nB,1),:) - fea_train).^2, 2))';
end
d = sqrt(d);
这篇关于MATLAB版本7中的pdist2等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!