adding vector entry after each element

1 回表示 (過去 30 日間)
Richard
Richard 2012 年 8 月 15 日
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 件)

Sean de Wolski
Sean de Wolski 2012 年 8 月 15 日
And just for fun:
filter(x(2:end),1,ones(1,numel(x)-1))+x(1)
  1 件のコメント
Matt Fig
Matt Fig 2012 年 8 月 15 日
編集済み: Matt Fig 2012 年 8 月 15 日
While we're on the topic of ridiculously inefficient alternatives to CUMSUM, ;-)
sum(triu(toeplitz(x(2:end))))+x(1)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Preprocessing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by