What is my error in for loop to find the cumulative sum?
Show older comments
for i=1:10 cumulativesum=cumulativesum+x(i) end
Answers (1)
Roger Stafford
on 28 Feb 2014
1 vote
To be safe, you need to set 'cumulativesum' to zero before starting in that for-loop. It might have some other value than zero from some previous operation, which would spoil the computation.
Also be aware that matlab has a function, 'cumsum', which will find the cumulative sum of a vector, as well as the cumulative sums of an array's rows or columns.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!