You can use NumPy broadcasting to let the elementwise multiplication happen in a vectorized manner after extending B
to 3D
after adding a singleton dimension at the end with np.newaxis
or its alias/shorthand None
. Thus, the implementation would be A*B[:,:,None]
or simply A*B[...,None]
.