Vectorization of For loop

2 次查看(过去 30 天)
Dear Matlab community,
Is it possible to vectorize the following for loop:
a = rand(100,100);
b = rand(500,100,100);
for i = 1:500
c = reshape(b(i, :, :),100,100);
d(i) = sum(sum(a.*c));
end
  14 个评论
Bruno Luong
Bruno Luong 2020-8-15
You could do a hybrid method: for-loop with each iteration compute a chunk of 50 elements of d.
MahdiH
MahdiH 2020-8-16
@ Bruno, Thanks for bringing the hybrid idea, I like it. Also, I'm aware that you explained the RAM issue, but I was telling Walter that the RAM limitation make the for loop my best bet.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2020-8-14
d = sum(b .* reshape(a, 1, 100, 100), [2 3]);

更多回答(1 个)

Bruno Luong
Bruno Luong 2020-8-14
d = b(:,:)*a(:)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by