フィルターのクリア

Vectorization of a nestled loop

3 ビュー (過去 30 日間)
AM
AM 2018 年 10 月 12 日
コメント済み: AM 2018 年 10 月 12 日
I am trying to improve the overall performance of my code and I am learning about vectorization. I've read the documentation on this but I'm having a hard time with even seemingly simple parts of the code. I have the following loop
dndt=zeros(m,1);
for i=1:n
for j=1:m
dndt(j,1)=dndt(j,1)+matA(i,j)*B(i);
end
end
Where matA is a n x m matrix and B is a vector of size n. I'm not sure how to do this.
Any tips or help is appreciated, thank you!

採用された回答

Stephen23
Stephen23 2018 年 10 月 12 日
編集済み: Stephen23 2018 年 10 月 12 日
matA.'*B(:)
which gives an output with size mx1, just like your nested loops. Even simpler:
B*matA
which gives an output with size 1xm.
  1 件のコメント
AM
AM 2018 年 10 月 12 日
It works perfectly, thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by