I have a 2D numpy array x and and 1D numpy array y:
import numpy as np
x = np.arange(12).reshape((4, 3))
y = np.array(([1.0,2.0,3.0,4.0])
I want to multiply / add the column vector y.reshape((4,1)) to each column of x. I attempted the following:
y1 = y.reshape((4,1))
y1 * x
yields
array([[ 0., 1., 2.],
[ 6., 8., 10.],
[ 18., 21., 24.],
[ 36., 40., 44.]])
which is what I wanted. I also found
array([[ 1., 2., 3.],
[ 5., 6., 7.],
[ 9., 10., 11.],
[ 13., 14., 15.]])
with y1 + x.
I would like to know if there is a better (more efficient) way to achieve the same thing!