growning data storage after each loop

조회 수: 3 (최근 30일)
Jo Betteley
Jo Betteley 2018년 3월 7일
편집: Rik 2018년 3월 8일
Hey,
Hopefully someone can put me out of my misery;
I have 2 for loops which represent cycles. The first for loop has cycles 1 to n, with the for loop inside of that calculating data 1:100. I wanted to store the calculated data as a running total for the cycle which i have managed with this code:
RT =0
For Y = 1:5
for x = 1:100
value(x) = 2*x;
if x >1
RT(x) = (value(x)) + RT(x-1)
end
end
end
what i cant seem to do which i need to do is, after each cycle i want the running total to carry on, so the last value of the previous cycle becomes the starting value for the next cycle. The end goal being a whole running total stored of as many cycles as i input.
Could someone please help.
Thank you

채택된 답변

Rik
Rik 2018년 3월 7일
Do you mean something like the code below? I'm assuming this is a small example and not your actual code, so I didn't bother optimizing this a lot.
RT =zeros(100,5);
for Y = 1:5
for x = 1:100
if x==1 && Y>1
RT(x,Y)=RT(end,Y-1);
end
value = 2*x;
if x ~=1
RT(x,Y) = value + RT(x-1,Y);
end
end
end
  댓글 수: 2
Jo Betteley
Jo Betteley 2018년 3월 8일
@Rik Wisselink. Yess! Thank you very much that worked perfectly. This was just a representation of the code to save on space.
Thank you again for the help.
Rik
Rik 2018년 3월 8일
편집: Rik 2018년 3월 8일
You're welcome

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

추가 답변 (0개)

카테고리

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