2

For instance I have a matrix and a vector of scalars

A = np.array([
    [1,2,3],
    [4,5,6],
    [7,8,9]
])

B = np.array([1, 2, 3])

The output that I want to get is like this, basically I want to multiply each row by scalar that have same index

 1   2   3
 8  10  12
21  24  27

1 Answers1

4

You can use broadcasting: A * B[:, None]:

array([[ 1,  2,  3],
       [ 8, 10, 12],
       [21, 24, 27]])
joni
  • 6,840
  • 2
  • 13
  • 20