Filter löschen
Filter löschen

Vector Matrix multiplication (Row wise)

81 Ansichten (letzte 30 Tage)
Kamuran
Kamuran am 16 Sep. 2015
Kommentiert: Noah Tang am 28 Okt. 2019
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

Akzeptierte Antwort

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

Weitere Antworten (3)

Vladimir Kazei
Vladimir Kazei am 9 Okt. 2017
Bearbeitet: Vladimir Kazei am 9 Okt. 2017
L = L .* V;

seif seif
seif seif am 26 Jan. 2018
Bearbeitet: seif seif am 26 Jan. 2018
I'd suggest a faster version than the above methods:
L = L .* v(:, ones(N,1));
  1 Kommentar
Noah Tang
Noah Tang am 28 Okt. 2019
Could you explain that why does this indexing trick work?

Melden Sie sich an, um zu kommentieren.


James Tursa
James Tursa am 16 Sep. 2015
L = bsxfun(@times,L,V);

Kategorien

Mehr zu Simulink finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by