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

1 Ansicht (letzte 30 Tage)
Christian Tieber
Christian Tieber am 2 Sep. 2019
I would like to do:
vectorB(n) = sum(vectorA(1:n))
without a loop.
Thanks in advance

Akzeptierte Antwort

madhan ravi
madhan ravi am 2 Sep. 2019
Bearbeitet: madhan ravi am 2 Sep. 2019
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)

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by