adding vector entry after each element

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

 采纳的回答

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

更多回答(1 个)

And just for fun:
filter(x(2:end),1,ones(1,numel(x)-1))+x(1)

1 个评论

While we're on the topic of ridiculously inefficient alternatives to CUMSUM, ;-)
sum(triu(toeplitz(x(2:end))))+x(1)

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Interpolation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by