Summing output of for loop.

7 次查看(过去 30 天)
Dougie
Dougie 2012-4-29
评论: Tilkesh 2017-4-14
I have a for loop producing an n x m matrix E fr every time the for loop runs. But i then need to sum all the output matrices from the for loop. Can anyone suggest how to do this. -I dont need to save the individual matrices, just the sum of them. Thanks
  1 个评论
Jan
Jan 2012-4-30
What have you tried and which problem has occurred? Actually the procedure is trivial: Create the new matrix in each iteration and sum it up.

请先登录,再进行评论。

回答(1 个)

Geoff
Geoff 2012-4-30
I gather you have the following situation, or something similar.
nloops = 100;
sumE = zeros(n,m);
for i = 1:nloops
% This loop generates a new E
E = [];
for j = 1:n*m
% magic goes here
end
% Now you want to sum E
sumE = sumE + E;
end
Well, the answer is contained above. =)
  5 个评论
Jan
Jan 2012-5-2
@Dougie: If you had posted the relevant part of your program, Geoff wouldn't has to guess the part for calculating E.
Beside the tiny detail of the pre-allocation, Geoff's solution seems to be optimal. +1
Tilkesh
Tilkesh 2017-4-14
Yes really it is magic

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Birthdays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by