Haven't benchmarked this (so this is not guaranteed to be faster), but here goes:
[L, ma, na] = size(A);
[L, mb, nb] = size(B);
AX = reshape(permute(A, [2 1 3]), [], na);
BX = reshape(permute(B, [2 3 1]), mb, []);
CX = reshape(permute(reshape(AX * BX, ma, L, nb, L), [1 3 2 4]), ma, nb, []);
C = permute(CX(:, :, 1:L + 1:end), [3 1 2]);
Note that you might also run into memory problems if A
and B
are large (in which case you'll have to resort to loops).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…