Cumulative sum for a vector

14 views (last 30 days)
Teemu on 2 Mar 2013
I have vector 1x5 (1,5,3,2,8) and I like to do cumulative sum of that. So the answer should seems like (6,9,11,19). Can you help me how to do it without loop?

Accepted Answer

Wayne King
Wayne King on 2 Mar 2013
Edited: Wayne King on 2 Mar 2013
x = [1,5,3,2,8];
but the first element is 1, not 6 as you have indicated. Of course, if you don't want that first element, you can do:
x = [1,5,3,2,8];
y = cumsum(x);
y = y(2:end);
  1 Comment
Teemu on 2 Mar 2013
Thanks, it was simple solution!

Sign in to comment.

More Answers (0)


Find more on File Name Construction 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!