I have two matrices
a = np.matrix([[1,2], [3,4]])
b = np.matrix([[5,6], [7,8]])
and I want to get the element-wise product, [[1*5,2*6], [3*7,4*8]]
, equaling
[[5,12], [21,32]]
I have tried
print(np.dot(a,b))
and
print(a*b)
but both give the result
[[19 22], [43 50]]
which is the matrix product, not the element-wise product. How can I get the the element-wise product (aka Hadamard product) using built-in functions?
a
andb
aren't NumPy's matrix type? With this class,*
returns the inner product, not element-wise. But for the usualndarray
class,*
means element-wise product. – bnaeckera
andb
numpy arrays? Also, in your question above, you are usingx
andy
for computation instead ofa
andb
. Is that just a typo? – jtitusj@
for matrix multiplication with numpy arrays, which means there should be absolutely no good reason to use matrices over arrays. – Praveena
andb
are lists. They will work innp.dot
; but not ina*b
. If you usenp.array(a)
ornp.matrix(a)
,*
works but with different results. – hpaulj