필터 지우기
필터 지우기

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