Make this matrix multiplication more efficient

1 回表示 (過去 30 日間)
Ignacio Echeveste
Ignacio Echeveste 2015 年 11 月 23 日
コメント済み: Ignacio Echeveste 2015 年 11 月 26 日
Hello,
I would like to do the following matrix multiplication much efficiently:
m=1000;n=500;
a=zeros(n,1);
b=rand(n,1);
A=rand(m,n);
B=rand(m,m);
for i=1:n
a(i)=b'*(A'*B(i,:)'*B(i,:)*A)*b;
end
Thanks in advance

採用された回答

James Tursa
James Tursa 2015 年 11 月 25 日
a = (B(1:n,:)*(A*b)).^2;
You dimensions for B look a little strange to me, since your calculations do not use all of the rows of B (hence the B(1:n,:) reduction above).
  1 件のコメント
Ignacio Echeveste
Ignacio Echeveste 2015 年 11 月 26 日
Yes, the dimensions were wrong. Thank you, it is much more efficient.

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

その他の回答 (1 件)

Richa Gupta
Richa Gupta 2015 年 11 月 25 日
Hi Ignacio,
The code below reduces the time from 2.6 secs to 0.06 secs on my machine:
m = 1000; n = 500;
a = zeros(n,1);
b = rand(n,1);
A = rand(m,n);
B = rand(m,m);
for i=1:n
temp =(B(i,:)*A)*b;
a(i) = temp'*temp;
end
Hope this helps.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by