# Fix Python – Multiplying across in a numpy array

## Question

I’m trying to multiply each of the terms in a 2D array by the corresponding terms in a 1D array. This is very easy if I want to multiply every column by the 1D array, as shown in the numpy.multiply function. But I want to do the opposite, multiply each term in the row.
In other words I want to multiply:

``````[1,2,3]   
[4,5,6] * 
[7,8,9]   
``````

and get

``````[0,0,0]
[4,5,6]
[14,16,18]
``````

``````[0,2,6]
[0,5,12]
[0,8,18]
``````

Does anyone know if there’s an elegant way to do that with numpy?
Thanks a lot,
Alex

Now we will see solution for issue: Multiplying across in a numpy array

Normal multiplication like you showed:

``````>>> import numpy as np
>>> m = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> c = np.array([0,1,2])
>>> m * c
array([[ 0,  2,  6],
[ 0,  5, 12],
[ 0,  8, 18]])
``````

If you add an axis, it will multiply the way you want:

``````>>> m * c[:, np.newaxis]
array([[ 0,  0,  0],
[ 4,  5,  6],
[14, 16, 18]])
``````

You could also transpose twice:

``````>>> (m.T * c).T
array([[ 0,  0,  0],
[ 4,  5,  6],
[14, 16, 18]])
``````

This question is answered By – jterrace