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

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 日

0 投票

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 件)

カテゴリ

ヘルプ センター および 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