I would like to do the following operation: I have a "4D" matrix A and a "3D" matrix B. Both A and B are actually 2D matrices, where for A, each element is a 2D matrix, and for B, each element is a 1D matrix or vector. I would like to, for each element in the 2D floor plan, multiply the matrix on A and the matrix in B. Graphically,
Currently, my code is:
% size(A) = Nx, Ny, n, n % size(B) = Nx, Ny, n % size(C) = Nx, Ny, n for k1 = 1 : Nx for k2 = 1 : Ny C(k1,k2,:) = squeeze(A(k1,k2,:,:)) * squeeze(B(k1,k2,:)); end end
I am looking for a way to "vectorize" the code and avoid loops completely. I am hoping such a vectorization will speed up my program.