How do I multiply a vector with a matrix element in numpy?

I have a multidimensional array whose shape is (32,3,5,5) and an array with shape (32). How can I multiply (i, 3,5,5) by (i,) for each i using numpy other than for loop?

+3


source to share


1 answer


With a

and b

as two arrays, several approaches can be suggested -



a*b[:,None,None,None]
a*b.reshape(-1,*[1]*3)
(a.T*b).T
np.einsum('i...,i->i...',a,b)

      

+2


source







All Articles