How to calculate a cumulative sum in a loop?

조회 수: 2 (최근 30일)
Sandy
Sandy 2013년 11월 20일
댓글: Walter Roberson 2013년 11월 21일
I have a loop that looks like this:
for a = 1: 9
for b = 1 : 1000
for c = 1 : 1000
image = myimage_bands(b, c, a);
new_image = image * 5;
end
end
end
I am trying to find the cumulative sum of my variable 'new_image', as it goes through the loop. I've tried cumsum, but it doesn't work.
Any ideas are appreciated. Thanks!
  댓글 수: 1
Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 20일
편집: Azzi Abdelmalek 2013년 11월 20일
There is no cumulative sum in your code, can you explain what you want?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2013년 11월 21일
Before the loop,
image_sum = [];
inside the loop after you have calculated new_image,
if isempty(image_sum)
image_sum = new_image;
else
image_sum(end+1) = image_sum(end) + new_image;
end
  댓글 수: 2
Sandy
Sandy 2013년 11월 21일
Thank you! I have another question though. How would I change the code if my variable new_image was a matrix (6 x 6)?
Walter Roberson
Walter Roberson 2013년 11월 21일
image_sum(:,:,end+1) = image_sum(:,:,end) + new_image;

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Design and Simulate SerDes Systems에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by