Search code examples
pythonnumpymatrix-multiplicationmultiplicationnumpy-ndarray

function for two numpy array costume multiplication


I need a simple numpy function do this multiplication without a for-loop and more time efficient.

Actually I want a function that multiplies each row of a to b

a=np.arange(2,12).reshape(5,2)
b=np.array([[1,2],[3,4]])
c=np.array([[a[i,:]@b] for i in range(a.shape[0])])

Solution

  • With numpy.tensordot:

    c = np.tensordot(a, b, axes=1)
    

    If you insist that shape will be the same:

    c.reshape(5,1,2)