How to vectorize a two matrices in row-wise multiplication (optimization)?

2 ビュー (過去 30 日間)
M G
M G 2013 年 12 月 1 日
編集済み: Jan 2013 年 12 月 1 日
Hi Matlab users,
I have the following code where N = 20000, "prt1" and "P" are both matrices of (20000x20000). As can be seen in the code I want to multiply each row of "prt1" from 1st to 20000th row in row 1 of "P" and then all rows of "prt1" in row 2 of "P" and so on. Every time sum them up and put in the matrix of "S". Unfortunately, This takes me great deal of time. Any way that I can optimize this such vectorization?
Here is the code:
for r = 1:N
for c = 1:N
S(r,c) = sum (prt1(c,:).*P(r,:));
end
end
Thank you very much for your hint.
All the best....
MhD
  2 件のコメント
Jan
Jan 2013 年 12 月 1 日
Did you pre-allocate S?
M G
M G 2013 年 12 月 1 日
Yes I did. However, still runs for hours!

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

採用された回答

Jan
Jan 2013 年 12 月 1 日
編集済み: Jan 2013 年 12 月 1 日
For N = 1000 I get 1.7 sec instead of 21 sec of the original version:
S = zeros(N, N);
for r = 1:N
S(r, :) = prt1 * P(r, :)';
end
But with 0.14 seconds even faster:
S = P * ptr1';
A speedup of factor 150, fine.

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by