Cumulative sum and cumulative product
Show older comments
Hi all
I'm running an optimization that involves a cumulative sum and a cumulative product. More formally, I would like to realize for each n elements of vector v:
v = zeros(1,n);
for i = 2:n
v(i) = v(i-1)*a(i-1) + b(i-1);
end
where a and b are the input vectors, and v is the output vector. Crucially, I'm not only interested in the last element of v but on each of those.
I would like to find an efficient way to compute this algorithm, since most of the optimization time is taken by this for loop.
Would it be feasible to write a simple function - as cumsum or cumprod - that would do the job?
Thanks in advance
VM
Accepted Answer
More Answers (0)
Categories
Find more on Problem-Based Optimization Setup 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!