[SOLVED] Multiplying each column of a d x n matrix with each column of a 1 x n vector

Issue

Suppose I have a 1 x 5 vector:

vect = np.array([10, 20, 30, 40, 50])

and I have a 2 x 5 matrix:

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

and I would like to multiply together as so: 10 * [[1], [6]] = [[10], [60]] for each column in the vector and each column in the matrix, outputting a d x n vector. How can I efficiently accomplish this with numpy? I have tried to investigate dot product, but it doesn’t seem to accomplish my goals.

Solution

vect * mat does what you want, column-wise multiplication.

Answered By – John Zwinck

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *