vectorB(n) = sum(vectorA(1:n)) without a loop

1 回表示 (過去 30 日間)
Christian Tieber
Christian Tieber 2019 年 9 月 2 日
コメント済み: Christian Tieber 2019 年 9 月 4 日
I would like to do:
vectorB(n) = sum(vectorA(1:n))
without a loop.
Thanks in advance

採用された回答

madhan ravi
madhan ravi 2019 年 9 月 2 日
編集済み: madhan ravi 2019 年 9 月 2 日
Perhaps you just want cumsum()
help cumsum
doc cumsum
Or you just need:
v= accumarray(n(:),n(:),[],@(x) sum(vectorA(1:x)));
Wanted = v(n);
% or
Wanted = arrayfun(@(x) sum(vectorA(1:x)),n)
  1 件のコメント
Christian Tieber
Christian Tieber 2019 年 9 月 4 日
cumsum looks good.
Thanks!

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by