adding vector entry after each element

4 views (last 30 days)
This is a very basic question but how would I add the elements of a vector like so...:
x = [1,2,3,4,5,6];
ans = 3 6 10 15 21

Accepted Answer

Matt Fig
Matt Fig on 15 Aug 2012
Edited: Matt Fig on 15 Aug 2012
x = [1,2,3,4,5,6];
y = cumsum(x)
y = y(2:end) % To match your example....

More Answers (1)

Sean de Wolski
Sean de Wolski on 15 Aug 2012
And just for fun:
filter(x(2:end),1,ones(1,numel(x)-1))+x(1)
  1 Comment
Matt Fig
Matt Fig on 15 Aug 2012
Edited: Matt Fig on 15 Aug 2012
While we're on the topic of ridiculously inefficient alternatives to CUMSUM, ;-)
sum(triu(toeplitz(x(2:end))))+x(1)

Sign in to comment.

Categories

Find more on File Name Construction in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by