Clear Filters
Clear Filters

Summing values in a loop

2 views (last 30 days)
James Crowe
James Crowe on 27 Oct 2017
Commented: James Crowe on 27 Oct 2017
Hi, how would I add values in a loop after iteration. eg. say values that came out were 1,2,3,4,5. I would want an array of 1,3,6,10,15. So 3rd number is first 3 added together and 5th is the first 5 etc. Thank you.
iiTerm = zeros(10,1)
for ii = 1:10
iiTerm(ii) = (((-1).^(ii-1)).*(((pi/4).^(2.*(ii-1))))./(factorial(2.*(ii-1))));
end

Accepted Answer

Torsten
Torsten on 27 Oct 2017
You mean
for ii = 1:10
iiTerm(ii) = (((-1).^(ii-1)).*(((pi/4).^(2.*(ii-1))))./(factorial(2.*(ii-1))));
end
iiSum = cumsum(iiTerm);
?
Best wishes
Torsten.
  1 Comment
James Crowe
James Crowe on 27 Oct 2017
Thats exactly it! thank you very much!

Sign in to comment.

More Answers (0)

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!