Vector Matrix multiplication (Row wise)

Hi, I need to multiply each row of very large matrix with a row of corresponding vector. I don't really want to use for loop for that, i.e.,
N=15000;
L=rand(N,N); V=rand(N,1);
for i=1:1:N
L(i,:)=V(i)*L(i,:);
end
is it possible to do this in vectorized way?
Thank you
Erdem

 Respuesta aceptada

Thorsten
Thorsten el 16 de Sept. de 2015
L = L.*repmat(V, [1 N]);

Más respuestas (3)

seif seif
seif seif el 26 de En. de 2018
Editada: seif seif el 26 de En. de 2018
I'd suggest a faster version than the above methods:
L = L .* v(:, ones(N,1));

2 comentarios

Noah Tang
Noah Tang el 28 de Oct. de 2019
Could you explain that why does this indexing trick work?
Sanders
Sanders el 17 de Dic. de 2024
Vladimir Kazei's version was significantly faster on my computer.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 16 de Sept. de 2015

Comentada:

el 17 de Dic. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by