How to multiply two matrices
Mostra commenti meno recenti
Hello,
For example I would like to multiply the following two matrices:
A = rand([10,10,10]);
B = rand([10,10,10]);
defined by:
for i=1:size(A,3)
C(:,:,i)= A(:,:,i)*B(:,:,i);
end
The question is: is this possible without the use of a for loop? Kind regards, Carlas
2 Commenti
umar siyab
il 24 Nov 2011
no its not possible without the use of loop because u have to multiply every element in the matrix with other matrix..
Jan
il 24 Nov 2011
What are the real dimensions in your problem? It matters if size(A, 3) is much larger or much smaller than size(A, 1) and size(A, 2).
Risposta accettata
Più risposte (3)
Titus Edelhofer
il 24 Nov 2011
Hi Carlas,
probably yes. But in this case probably the loop is not your worst option, as long as you initialize
C = zeros(size(A,2), size(B,1), size(A,3));
before the loop.
Titus
1 Commento
Jan
il 24 Nov 2011
I do not completely agree with you, Titus: Instead of "not the worst" I'd claim, it is at least "very good". +1
James Tursa's MTIMESX (http://www.mathworks.com/matlabcentral/fileexchange/25977-mtimesx-fast-matrix-multiply-with-multi-dimensional-support ) can handle such tasks efficiently.
carlas
il 24 Nov 2011
0 voti
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!