MATLAB: 3D Matrix Multiply

matricesmatrix arraymatrix manipulationmultipl

  1. Assume a vector V which is (P X 1) array.
  2. Assume matrices A,B which is (M X N X P) array.
I want to multiply A(i, j, 🙂 .* V for each 0 < i <= M , and 0 < j <= N.
Currently I'm using loops to make it happen:
for ii = 1 : M
for jj = 1 : N
B(ii, jj, 🙂 = V.* squeeze(A(ii, jj, :));
end
end
Is there any way to do so which doesn't include any use of loops?

Best Answer

B = A .* reshape(V, [1, 1, numel(V)])
This works with auto-expanding since R2016b.