Fast matrix multiplication in loop
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Batuhan
am 7 Feb. 2014
Bearbeitet: Matt J
am 7 Feb. 2014
Dear All,
I have two matrices with dimensions 3x3 and E6x3. I need to multiply each row of the latter with the former. It's like
a=rand(3,3);
b=(1000000,3);
for i=1:size(b,1)
c=a*b(i,:)';
end
However, this step takes hours to be done. I wonder if there is any faster way to do this.
Cheers.
1 Kommentar
Akzeptierte Antwort
Azzi Abdelmalek
am 7 Feb. 2014
Bearbeitet: Azzi Abdelmalek
am 7 Feb. 2014
a=rand(3,3);
b=rand(100,3);
n=size(a,2);
m=size(b,1);
c=zeros(m,n);
for i=1:size(b,1)
c(i,:)=a*b(i,:)';
end
%or simply
c=(a*b')'
0 Kommentare
Weitere Antworten (2)
Jos (10584)
am 7 Feb. 2014
Two options:
1. pre-allocate C to avoid memory allocation in each iteration
C = zeros(N, ..) % pre-allocation
for k = 1:N,
C(k,:) = ..
end
2. Use BSXFUN
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!