How to Calculate Moving Product?

4 visualizaciones (últimos 30 días)
Gaurav Soni
Gaurav Soni el 18 de Dic. de 2015
Comentada: Gaurav Soni el 8 de En. de 2020
Is there any vectorized way of calculating moving products. If A is a vector, how can I calculate its moving product with a specified window size.
For example, for a window size of 3, the moving product should be equal to:
[A(3)*A(2)*A(1), A(4)*A(3)*A(2), A(5)*A(4)*A(3), ..., A(N)*A(N-1)*A(N-2)]
How do I calculate it without using a for loop. I know that the 'filter' function can be used for calculating moving means in a vectorized manner; but I have not seen any function that can calculate a moving product.
Can someone please help. Thanks.
  1 comentario
Guido Marco
Guido Marco el 27 de Jun. de 2016
try somthing like this... function Y=divf(X) % divisive differential
X0=X(1:end-1); X1=X(2:end); Y=X1./X0;

Iniciar sesión para comentar.

Respuesta aceptada

Jonathan Agg
Jonathan Agg el 6 de En. de 2020
The function movprod was added in 17a. There's also a list of similar functions here which are included in MATLAB, like movsum and movmax.

Más respuestas (3)

Guillaume
Guillaume el 18 de Dic. de 2015
One way to achieve what you want:
function mp = movingproduct(v, windowsize)
%add input validation code
vv = repmat([v(:); nan], 1, windowsize);
vv = reshape(vv(1:end-windowsize), [], windowsize);
mp = prod(vv(windowsize:end, :), 2);
if isrow(v), mp = mp.'; end
end
  2 comentarios
Gaurav Soni
Gaurav Soni el 18 de Dic. de 2015
Thank you. This is a nice method to avoid a For loop. I was also able to find a way. Here it is:
function mp = movingproduct(v, windowsize)
logv = log(v);
b = (1/windowsize)*ones(1,windowsize);
mp = exp(windowsize*filter(b,1,logv));
mp = mp(windowsize:end);
end
This method is a bit faster than what you devised above, but it suffers from numerical errors that accumulate due to log and exp operations.
Guillaume
Guillaume el 19 de Dic. de 2015
Editada: Guillaume el 21 de Mayo de 2016
Another possible implementation, which has no numerical errors. It does have a loop (through arrayfun) but only over the window size.
function mp = movingproduct(v, windowsize)
%add input validation code
vv = repmat({v(:)}, 1, windowsize);
vv = arrayfun(@(row) circshift(vv{row}, row-1), 1:windowsize, 'UniformOutput', false);
vv = [vv{:}];
mp = prod(vv(windowsize:end, :), 2);
if isrow(v), mp = mp.'; end
end

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 19 de Dic. de 2015
One possibility is to use nlfilter() where you can define the filter to be used on the window to do absolutely anything you want it to do, such as prod().
output = nlfilter(yourMatrix, [1, 3], @prod);
I attach a full blown demo where I use nlfilter to compute the Otsu threshold on a sliding 3x3 window basis.
  1 comentario
Gaurav Soni
Gaurav Soni el 19 de Dic. de 2015
Editada: Gaurav Soni el 19 de Dic. de 2015
Thanks. I think this would be pretty useful for people who have access to Image Processing toolbox. It looks like nlfilter is only available in the Image Processing Toolbox.

Iniciar sesión para comentar.


Mark Britten-Jones
Mark Britten-Jones el 21 de Mayo de 2016
Editada: Guillaume el 21 de Mayo de 2016
The problem with the loop approach is that it repeats calculations. For example A(3)*A(2) is calculated twice in a moving product of window size 3. The vectorised versions also suffer from this repetition. It can be avoided using a recursive approach which recognises that e.g. for k even a k moving product can be calculates as a k/2 moving product times the k/2 lag of the k/2 moving product. For large k the increases in speed are dramatic. Here is the code, which relies on a lag function (few lines to code up):
function y = cumprodk(x,k)
y = x;
if k==1; return; end;
y = cumprodk(y, floor(k/2));
y = lag(y, floor(k/2)).*y;
if rem(k,2) == 1 % if k odd need to multiply by lag k-1
y = lag(x,k-1).*y;
end
end
Thats it. No loops and faster for large k

Categorías

Más información sobre Install Products en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by