-2

A and B matrices will be different when i run the program

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

The output matrix (C) should be the same dimension as matrix A. As title says, I'm trying to multiply each row from one matrix (A) to every rows to another matrix (B) and would like to sum them.

For example, Dimension of C = (2,3)

C = [[A(0)*B(0) + A(1)*B(0)], [A(0)*B(1) + A(1)*B(1)],[A(0)*B(1) + A(1)*B(1)]]

I would like to know if there is a numpy function does that.

2

1 Answer 1

1

Use numpy broadcasting:

C = (A * B[:, None]).sum(axis=1)

Output:

>>> C
array([[3, 3, 3],
       [6, 6, 6],
       [9, 9, 9]])
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.