Vector Matrix multiplication (Row wise)

60 visualizzazioni (ultimi 30 giorni)
Kamuran
Kamuran il 16 Set 2015
Commentato: Sanders il 17 Dic 2024
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

Risposta accettata

Thorsten
Thorsten il 16 Set 2015
L = L.*repmat(V, [1 N]);

Più risposte (3)

Vladimir Kazei
Vladimir Kazei il 9 Ott 2017
Modificato: Vladimir Kazei il 9 Ott 2017
L = L .* V;

seif seif
seif seif il 26 Gen 2018
Modificato: seif seif il 26 Gen 2018
I'd suggest a faster version than the above methods:
L = L .* v(:, ones(N,1));
  2 Commenti
Noah Tang
Noah Tang il 28 Ott 2019
Could you explain that why does this indexing trick work?
Sanders
Sanders il 17 Dic 2024
Vladimir Kazei's version was significantly faster on my computer.

Accedi per commentare.


James Tursa
James Tursa il 16 Set 2015
L = bsxfun(@times,L,V);

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by