each columun of a 2D matrix multiply with a 3D array
Mostra commenti meno recenti
Hi,
I have a 2D matrix and a 3D array, I want to perform the following calculations:
A=rand(200,300);
B=rand(60,60,200);
c=0;
for j=1:300
d=0;
for k=1:200
d=d+A(k,j)*B(:,:,k);
end
c=c+d;
end
The a two for loops, the calculations are very slowly. Are there some good methods to aviod the loops?
Risposta accettata
Più risposte (1)
A=rand(20,30);
B=rand(6,6,20);
c=0;
for j=1:30
d=0;
for k=1:20
d=d+A(k,j)*B(:,:,k);
end
c=c+d;
end
whos
c
c=0;
for j=1:30
c = c + sum(reshape(A(:, j), 1, 1, []).*B, 3);
end
c
1 Commento
Qiu Xu
il 23 Ott 2021
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!