-2

I find a strange problem in numpy: if m is a matrix, the results of m1*m2 is always the same as m1.dot(m2)!!! So how can I multipy two matrixes by elements?(such as m1.*m2 in matlab)

Shane
  • 65
  • 2

2 Answers2

1

If you multiply matrices (of type numpy.matrix), NumPy assumes you want matrix multiplication, which doesn't really seem that strange to me. To multiply element-wise, either use arrays (numpy.array) instead of matrices, or use numpy.multiply().

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
0

This is by-design. Link

For matrix, '*' means matrix multiplication, and the multiply() function is used for element-wise multiplication.

e.g.

>>> import numpy
>>> numpy.multiply([[1, 2], [3, 4]], [[5, 6], [7, 8]])
array([[ 5, 12],
       [21, 32]])
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005