How to remove for-loop in computing a vector of sums?

1 visualización (últimos 30 días)
Paul Hoffrichter
Paul Hoffrichter el 8 de Dic. de 2020
Comentada: Paul Hoffrichter el 8 de Dic. de 2020
How to compute rcross vector without the for-loop?
rcross = zeros( length(elementProduct) - prefixLen , 1 );
for ii = 1: length(elementProduct) - prefixLen
rcross(ii) = sum( elementProduct(ii : ii + prefixLen - 1) );
end
In one scenario prefixLen is 8. length(elementProduct) can vary from a few hundred to a few million.
  6 comentarios
Paul Hoffrichter
Paul Hoffrichter el 8 de Dic. de 2020
@KSSV,
Thank you for your comment. If you move it to an answer, I will accept it. Thanks again!
~Paul
Paul Hoffrichter
Paul Hoffrichter el 8 de Dic. de 2020
I think your approach will also work. But I believe (without testing) that using conv would be slower than movsum.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by