Adding the values of a vector together and creating a vector comprised of its sums

1 回表示 (過去 30 日間)
So I have a column vector s, the length of the vector can change and the values inside are randomly either +1 or -1.
for this question lets say that the vector is 3 values long. N = 3 (where N is the number of values in the vector s)
and say our randomised vector gives us
s = [1; 1; -1; ];
what i would like to do is to add the values in the vector together (from left to right order)
and then create a column vector of the values as they are added. so in this example
1 1+1 2+ -1
therefore the column vector should come out as x = [1; 2; 1; ];
Can someone help me do this? Many thanks.

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 11 月 8 日
編集済み: Ameer Hamza 2020 年 11 月 8 日
See cumsum().
s = [1; 1; -1; ];
x = cumsum(s)
Result
>> x
x =
1
2
1
  2 件のコメント
Ben Newton
Ben Newton 2020 年 11 月 8 日
Thank you very much
Ameer Hamza
Ameer Hamza 2020 年 11 月 8 日
I am glad to be of help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by