How to vectorize the for loop

Dear all,
I am trying to vectorize the following statement:
u=zeros(n,2,2);
v=zeros(N);
w=zeros(n,2,2);
for j=1:n
u(j,1,1)=v(j)*w(j,1,1);
end
The vectorized version:
j=1:n
u(j,1,1)=v(j)*w(j,1,1);
But I got the error message:
Inner matrix dimensions must agree
because N>n.
Would you please suggest a way to resolve this problem? Thank you.

 Respuesta aceptada

Kelly Kearney
Kelly Kearney el 2 de Dic. de 2013

0 votos

u(j,1,1) = v(j) .* w(j,1,1);
The .* means element-by-element multiplication; otherwise, it attempts matrix multiplication.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 2 de Dic. de 2013

Respondida:

el 2 de Dic. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by