Make this matrix multiplication more efficient

1 vue (au cours des 30 derniers jours)
Ignacio Echeveste
Ignacio Echeveste le 23 Nov 2015
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

Réponse acceptée

James Tursa
James Tursa le 25 Nov 2015
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 commentaire
Ignacio Echeveste
Ignacio Echeveste le 26 Nov 2015
Yes, the dimensions were wrong. Thank you, it is much more efficient.

Connectez-vous pour commenter.

Plus de réponses (1)

Richa Gupta
Richa Gupta le 25 Nov 2015
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.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by