我有以下(慢)代码:
% A is n-by-m matrix
% B is n-by-m-by-d matrix
% C is n-by-m-by-d matrix
% R is 1-by-d vector
A=zeros(n,m);
for i=1:d
A = A + sum(B(:,:,1:i),3).*(R(i)-C(:,:,i));
end
我想通过使用神奇的bsxfun来失去循环来提高效率。你能告诉我怎么做吗?
答案 0 :(得分:4)
这样 -
A = sum(cumsum(B,3).*bsxfun(@minus,permute(R,[1 3 2]),C),3)
每个大小参数n,m,d
为200
,运行时间为
----------------------------------- With Proposed Approach
Elapsed time is 0.054771 seconds.
----------------------------------- With Original Approach
Elapsed time is 2.514884 seconds.
使用bsxfun
和vectorization
的更多理由!!
基准代码 -
n = 10;
m = 1000;
d = 3;
num_iter = 10000;
B = rand(n,m,d);
C = rand(n,m,d);
R = rand(1,d);
%// Warm up tic/toc.
for k = 1:100000
tic(); elapsed = toc();
end
disp(['********************* d = ' num2str(d) ' ************************'])
disp('----------------------------------- With Proposed Approach')
tic
for iter = 1:num_iter
A1 = sum(cumsum(B,3).*bsxfun(@minus,permute(R,[1 3 2]),C),3);
end
toc
disp('----------------------------------- With Original Approach')
tic
for iter = 1:num_iter
A = zeros(n,m);
for i=1:d
A = A + sum(B(:,:,1:i),3).*(R(i)-C(:,:,i));
end
end
toc
我的运行时间是 -
********************* d = 3 ************************
----------------------------------- With Proposed Approach
Elapsed time is 0.856972 seconds.
----------------------------------- With Original Approach
Elapsed time is 1.703564 seconds.
********************* d = 9 ************************
----------------------------------- With Proposed Approach
Elapsed time is 2.098253 seconds.
----------------------------------- With Original Approach
Elapsed time is 9.518418 seconds.