vectorB(n) = sum(vectorA(1:n)) without a loop
1 view (last 30 days)
Show older comments
Christian Tieber
on 2 Sep 2019
Commented: Christian Tieber
on 4 Sep 2019
I would like to do:
vectorB(n) = sum(vectorA(1:n))
without a loop.
Thanks in advance
0 Comments
Accepted Answer
madhan ravi
on 2 Sep 2019
Edited: madhan ravi
on 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)
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!