MATLAB: Cumulative sum and cumulative product

cumprodcumsum

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

Best Answer

Take a look at the FILTER function.
Related Question