Vector Matrix multiplication (Row wise)

조회 수: 39 (최근 30일)
Kamuran
Kamuran 2015년 9월 16일
댓글: Sanders 2024년 12월 17일
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

채택된 답변

Thorsten
Thorsten 2015년 9월 16일
L = L.*repmat(V, [1 N]);

추가 답변 (3개)

Vladimir Kazei
Vladimir Kazei 2017년 10월 9일
편집: Vladimir Kazei 2017년 10월 9일
L = L .* V;

seif seif
seif seif 2018년 1월 26일
편집: seif seif 2018년 1월 26일
I'd suggest a faster version than the above methods:
L = L .* v(:, ones(N,1));
  댓글 수: 2
Noah Tang
Noah Tang 2019년 10월 28일
Could you explain that why does this indexing trick work?
Sanders
Sanders 2024년 12월 17일
Vladimir Kazei's version was significantly faster on my computer.

댓글을 달려면 로그인하십시오.


James Tursa
James Tursa 2015년 9월 16일
L = bsxfun(@times,L,V);

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by