vectorB(n) = sum(vectorA(1:n)) without a loop
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Christian Tieber
il 2 Set 2019
Commentato: Christian Tieber
il 4 Set 2019
I would like to do:
vectorB(n) = sum(vectorA(1:n))
without a loop.
Thanks in advance
0 Commenti
Risposta accettata
madhan ravi
il 2 Set 2019
Modificato: madhan ravi
il 2 Set 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)
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!