Cumulative sum for a vector

I have vector 1x5 (1,5,3,2,8) and I like to do cumulative sum of that. So the answer should seems like (6,9,11,19). Can you help me how to do it without loop?

 採用された回答

Wayne King
Wayne King 2013 年 3 月 2 日
編集済み: Wayne King 2013 年 3 月 2 日

1 投票

x = [1,5,3,2,8];
cumsum(x)
but the first element is 1, not 6 as you have indicated. Of course, if you don't want that first element, you can do:
x = [1,5,3,2,8];
y = cumsum(x);
y = y(2:end);

1 件のコメント

Teemu
Teemu 2013 年 3 月 2 日
Thanks, it was simple solution!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLicensing on Cloud Platforms についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by