MATLAB Answers


Cumulative sum for a vector

Asked by 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?




No products are associated with this question.

1 Answer

Answer by Wayne King
on 2 Mar 2013
Edited by Wayne King
on 2 Mar 2013
 Accepted answer

 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

Thanks, it was simple solution!

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!