MATLAB Answers

0

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

Asked by Christian Tieber on 2 Sep 2019
Latest activity Commented on by 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

Sign in to comment.

Products


Release

R2019a

1 Answer

Answer by madhan ravi
on 2 Sep 2019
Edited by madhan ravi
on 2 Sep 2019
 Accepted Answer

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 Comment

cumsum looks good.
Thanks!

Sign in to comment.