Vector Matrix multiplication (Row wise)

160 ビュー (過去 30 日間)
Kamuran
Kamuran 2015 年 9 月 16 日
コメント済み: Noah Tang 2019 年 10 月 28 日
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]);
  1 件のコメント
Kamuran
Kamuran 2015 年 9 月 16 日
Thank you

サインインしてコメントする。

その他の回答 (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));
  1 件のコメント
Noah Tang
Noah Tang 2019 年 10 月 28 日
Could you explain that why does this indexing trick work?

サインインしてコメントする。


James Tursa
James Tursa 2015 年 9 月 16 日
L = bsxfun(@times,L,V);

カテゴリ

Help Center および File ExchangeSimulink についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by