matrix products along a three dimensional array
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a three dimensional array that contains instances of square matrices in its first two dimensions. I would like to take products of these matrices along the third dimension without using a loop. I need matrix products, not element-wise products. Is this possible to do using indexing?
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 15 Okt. 2011
To check, you want
A(:,:,1) * A(:,:,2) * A(:,:3) * A(:,:,4) and so on ?
(Or should it be the other order, since the multiplication is not commutative) ?
If so, then this will be tricky without a loop. I am not sure at the moment that it could be done without a loop... possibly by a wretched use of accumarray.
Could it be done using indexing? Hmmm -- if you had the exact matrix size ahead of time, then you could compute the product symbolically, creating a long string of multiplications and additions for each element, and then at run time you could substitute in the actual array values in place at the symbols. I can assure you, though, that such a method would be a lot slower than a simple loop.
If you are trying to avoid a loop because "loops are slow" then in this case it would be a false economy. 2D matrix multiplication of large matrices is implemented just about as quickly as you can get (okay, maybe not quite as quickly as theoretically possible, but the top theoretical speed needs a lot of spare memory!). The loop would add trivial overhead to all of the multiplications and additions that would have to be done.
2 Kommentare
Walter Roberson
am 15 Okt. 2011
prod() does not do matrix multiplication.
Please clarify whether you want mathematical matrix multiplication (the '*' operator, also known as "inner product"), or if you just want to multiply all the elements in corresponding locations. Your specific indication that you do not want element-wise multiplication seemed to rule out the corresponding-elements version, but if that _is_ what you want after all, then
prod(A,3)
would do that along the third dimension.
Weitere Antworten (0)
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!