MATLAB 版本 7 中的 pdist2 等效项 [英] pdist2 equivalent in MATLAB version 7

查看:27
本文介绍了MATLAB 版本 7 中的 pdist2 等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在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.

任何帮助.

问候,

巴夫亚

推荐答案

您可以通过重复 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆