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

$\begingroup$

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

$\endgroup$ 1

1 Answer

$\begingroup$

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)
$\endgroup$

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like