Emma
Emma

Reputation: 618

adding elements of a vector

How is it possible to add an element in a vector to the previous elements. For example:

data = [1,3,5,6,7,9];
newData = [1,4,9,15,22,31];

How would I get from data to newData? I want to keep the first element, then add this to the second, add the combination of these to the third and so on.

Upvotes: 1

Views: 996

Answers (1)

Anonymous
Anonymous

Reputation: 18631

Try the cumsum function:

newData = cumsum(data)

Upvotes: 2

Related Questions