# Why doesn't multiplying two matrices with NumPy work sometimes? [closed]

Using NumPy and attempting to multiply matrices together sometimes doesn't work. For example

import numpy as np

x = np.matrix('1, 2; 3, 8; 2, 9')
y = np.matrix('5, 4; 8, 2')

print(np.multiply(x, y))


can return

Traceback (most recent call last):
File "vector-practice.py", line 6, in <module>
print(np.multiply(x, y))
ValueError: operands could not be broadcast together with shapes (3,2) (2,2)


I understand that I can't multiply these shapes, but why not? I can multiply these two matrices on paper, so why not in NumPy? Am I missing something obvious here?

• Welcome to scicomp.SE. This site is intended for questions combining science and computation and for this type of pure package problem Stackoverflow may be more appropriate. However the clue to your problem is the definition of numpy.multipy "Multiply arguments element-wise". You may be intending matrix multiplication, which is provided by numpy.dot instead. – origimbo Sep 13 '17 at 22:54

## 1 Answer

Use np.dot() not np.multiply().

np.multiply() is for element-wise multiplication.

• I think elementwise is a better choice of wording. – percusse Sep 14 '17 at 10:27