create from a vector a vector of cumulative sums

2 views (last 30 days)
Consider a vector
[e1, e2, e3, e4,...]
I want to create a vector
[e1, e1+e2, e1+e2+e3,...]
Is there any simpler way rather than writing a loop?

Accepted Answer

KSSV
KSSV on 29 May 2020
Edited: KSSV on 29 May 2020
Read about cumsum.
A = rand(10,1) ;
iwant = cumsum(A)

More Answers (1)

Alan Stevens
Alan Stevens on 29 May 2020
cumsum does exactly that. For example:
>> x = [1 3 5 7 9];
>> cumsum(x)
ans =
1 4 9 16 25

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!