How to get final iteration when using for loops

I am having trouble displaying the final iteration of my cumsum hardcode. here it is. Thanks
if true
% code
end
x=randi(10,1,10)
n=0;
cumu=0;
for n=1:length(x);
cumu=x(n)+cumu;
x(n)=cumu
end

1 Comment

What specifically do you want to display on the last iteration?

Sign in to comment.

Answers (1)

Your code as written (adding an end that you omitted) will do what you want:
x=randi(10,1,10);
n=0;
cumu=0;
for n=1:length(x)
cumu=x(n)+cumu;
end
x(n)=cumu
The final value of ‘n’ will remain defined after the loop, so ‘x(n)’ will be (in your code here) ‘x(10)’.
Also, just writing:
cumu
after the loop will do the same.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 9 Dec 2017

Answered:

on 9 Dec 2017

Community Treasure Hunt

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

Start Hunting!