Multiplication of 3D matrix by 2D matrix in Matlab without using loops

710 Views Asked by At

Want to multiply a $3D$ matrix $A$ of size $n$ x $m$ x $p$, ($n$: number of raws, $m$: number of columns and $p$: number of slices), by a 2D matrix $B$ of size $m$ x 1 x $p$. The results as shown in the figure below is a matrix $C$ of size $n$ x 1 x $p$. How to perform this on Matlab without using loops ?

enter image description here

1

There are 1 best solutions below

0
On

Not sure it is the good place for asking such question. I suppose that you want to compute the output matrix $\mathbf{C}$ as

C(:,:,p)=A(:,:,p)*B(:,:,p) 

using the matrix product. You can easily do this using implicit broadcasting

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