eyeballfrog
eyeballfrog

Reputation: 225

Multiplying arrays of matrices in MATLAB

I have a data set that consists of two 1800 x 900 x 3 x 3 arrays, which should each be interpreted as a 1800x900 array of 3x3 matrices. As part of the analysis, at one point I need to create another such array by, at each point of the 1800x900 array, multiplying the corresponding 3x3 matrices together.

There seem to be two ways to do this that I can think of. The obvious way is

C = zeros(size(A))
for i = 1:900
    for j = 1:1800
        C(i,j,:,:) = A(i,j,:,:)*B(i,j,:,:)
    end
end

But that's quite a long loop and doesn't really take advantage of MATLAB's vectorization. The other way is

C = zeros(size(A))
for i = 1:3
    for j = 1:3
        for k = 1:3
            C(:,:,i,j) = C(:,:,i,j) + A(:,:,i,k).*B(:,:,k,j)
        end
    end
end

where the large dimensions are getting vectorized and I'm basically using the for loops to implement the Einstein summation convention. This seems really inelegant, though, which makes me think there should be a better way. Is there?

Upvotes: 4

Views: 105

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112759

Perfect job for bsxfun with permute:

C = permute(sum(bsxfun(@times, A, permute(B, [1 2 5 3 4])), 4), [1 2 3 5 4]);

In R2016b onwards you can avoid bsxfun thanks to implicit singleton expansion:

C = permute(sum(A .* permute(B, [1 2 5 3 4]), 4), [1 2 3 5 4]);

Upvotes: 4

Related Questions