Vectorize many matrix multiplications using the third dimension

Asked by Alec Poulin

Alec Poulin (view profile)

on 17 Apr 2019
Latest activity Commented on by Alec Poulin

Alec Poulin (view profile)

on 25 Apr 2019
Accepted Answer by James Tursa

James Tursa (view profile)

I have collections of matrices, say two collections A and B of n matrices each, implemented as cell arrays. I would like to produce a third collection C by multiplying the matrices from the first two collections:
C = cell(1, n);
for i = 1:n
C{i} = A{i} * B{i};
end
Is it possible instead to use 3D arrays to vectorize this operation? Suppose that A and B are m x m x n arrays, where the ith page (where i ranges from 1 to n) of the arrays are the same as the ith cell in the previous example. Then, is it possible to write something that would look like
C = A * B;
to perform the multiplications in a way that is transparent for the pages of A and B?

Tags

Answer by James Tursa

on 17 Apr 2019

Alec Poulin

Alec Poulin (view profile)

on 23 Apr 2019
Your function does exactly what I need and I was able to compile it without too much trouble. Thank you a lot!
Alec Poulin

Alec Poulin (view profile)

on 25 Apr 2019
Another question: Would there be a way to do the same thing with mldivide? I saw that there are pre-operations available for the matrices, but there is no inverse among them.