Adam Adam - 4 months ago 21
Python Question

how to multiply 2 numpy array with different dimensions

I try to multiply 2 matrix x,y with shape (41) and (41,6)
as it is supposed to broadcast the single matrix to every arrow in the multi-dimensions

I want to do it as :


but i get this error

ValueError: operands could not be broadcast together with shapes (41,6) (41,)

Is there anything I miss here to make that possible ?


Not exactly sure, what you are trying to achieve. Maybe you could give an example of your input and your expected output. One possibility is:

import numpy as np

x = np.array([[1, 2], [1, 2], [1, 2]])
y = np.array([1, 2, 3])
res = x * np.transpose(np.array([y,]*2))

This will multiply each column of x with y, so the result of the above example is:

array([[1, 2],
       [2, 4],
       [3, 6]])