vectorB(n) = sum(vectorA(1:n)) without a loop
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Christian Tieber
am 2 Sep. 2019
Kommentiert: Christian Tieber
am 4 Sep. 2019
I would like to do:
vectorB(n) = sum(vectorA(1:n))
without a loop.
Thanks in advance
0 Kommentare
Akzeptierte Antwort
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)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!