Clear Filters
Clear Filters

Summing elements of a vector sequentially?

4 views (last 30 days)
Is there a function that will sum a vector in the following way?
Let x be a 1 x N vector. The ith element of the output vector should be sum(x(1:i)), where i ranges from 1 to N.
I will use a for loop, if I must...
x = 1:100;
output = zeros(1,length(x))
for i = 1:length(x), output(i) = sum(x(1:i)); end
Thanks!

Accepted Answer

per isakson
per isakson on 31 May 2015
See cumsum
  1 Comment
James
James on 31 May 2015
I had a feeling there would be a function dedicated to this. Thanks!

Sign in to comment.

More Answers (0)

Categories

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

Tags

Products

Community Treasure Hunt

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

Start Hunting!