For elementwise multiplication of matrix objects, you can use numpy.multiply:
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
np.multiply(a,b)
Result
array([[ 5, 12],
[21, 32]])
However, you should really use array instead of matrix. matrix objects have all sorts of horrible incompatibilities with regular ndarrays. With ndarrays, you can just use * for elementwise multiplication:
a * b
If you're on Python 3.5+, you don't even lose the ability to perform matrix multiplication with an operator, because @ does matrix multiplication now:
a @ b # matrix multiplication
Answer from Rahul K P on Stack OverflowFor elementwise multiplication of matrix objects, you can use numpy.multiply:
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
np.multiply(a,b)
Result
array([[ 5, 12],
[21, 32]])
However, you should really use array instead of matrix. matrix objects have all sorts of horrible incompatibilities with regular ndarrays. With ndarrays, you can just use * for elementwise multiplication:
a * b
If you're on Python 3.5+, you don't even lose the ability to perform matrix multiplication with an operator, because @ does matrix multiplication now:
a @ b # matrix multiplication
just do this:
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
a * b
Element-wise multiply arrays with different axes [numpy]
Are you using arrays? If yes, you're in luck because the '*' is element by element multiplication and not matrix multiplication. Therefore:
import numpy as np
A = np.array([5, 4, 3, 2, 1])
B = np.array([[ 123., 4., 0., 0., 0.],
[ 0., 0., 0., 45., 0.],
[ 0., 0., 100., -5., 0.]])
C = np.tile(A, (M, 1)) * B
D = A * B And as result you get:
>>> C
array([[ 615., 16., 0., 0., 0.],
[ 0., 0., 0., 90., 0.],
[ 0., 0., 300., -10., 0.]])
>>> D
array([[ 615., 16., 0., 0., 0.],
[ 0., 0., 0., 90., 0.],
[ 0., 0., 300., -10., 0.]])And if you're working with matrices, use multiply() to have element-wise multiplication (which is what you're trying to do)
https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html#array-or-matrix-which-should-i-use
More on reddit.comHow to do elementwise multiplication of two vectors using NumPy?
Multiply array times itself element wise in python?
What do you think is going on under the hood besides looping through and squaring every element in the array, in any language? In some languages you could end up lazily-evaluating it so the squaring isn't done on a given element until that element is requested, but otherwise there's no way to get around the fact that you have to square every single element one at a time.
In any case, if you just want a way to do it in fewer lines, you could do this:
map(lambda x: x*x, my_array)
or
[x*x for x in my_array]More on reddit.com
Composition of element-wise multiplication and lmul!
Videos
Hi everyone,
I'm trying to achieve something with the least possible overhead. I have a one-dimensional array A whose shape is (N,) and another one B whose shape is (M,N). What I'm trying to do is to element-wise multiply each column of B (axis 1) by A. Basically something like this:
C = np.zeros((M,N))
for m in range(M):
C[m,:] = A*B[m,:]This can also be achieved more succintly with:
C = np.tile(A, (M, 1))*B
The problem with the first approach is that it requires a for, and the problem with the second is that is unnecessarily creates a temporary array. I was wondering if there was some way to do this with some funky numpy stuff, perhaps with some tricky broadcasting but I haven't been able to come up with anything. Hopefully you will!!
Are you using arrays? If yes, you're in luck because the '*' is element by element multiplication and not matrix multiplication. Therefore:
import numpy as np
A = np.array([5, 4, 3, 2, 1])
B = np.array([[ 123., 4., 0., 0., 0.],
[ 0., 0., 0., 45., 0.],
[ 0., 0., 100., -5., 0.]])
C = np.tile(A, (M, 1)) * B
D = A * B
And as result you get:
>>> C
array([[ 615., 16., 0., 0., 0.],
[ 0., 0., 0., 90., 0.],
[ 0., 0., 300., -10., 0.]])
>>> D
array([[ 615., 16., 0., 0., 0.],
[ 0., 0., 0., 90., 0.],
[ 0., 0., 300., -10., 0.]])
And if you're working with matrices, use multiply() to have element-wise multiplication (which is what you're trying to do)
https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html#array-or-matrix-which-should-i-use
If you don't want to used np.tile, you can use np.newaxis:
>>> A = np.arange(30).reshape((10, 3))
>>> B = np.arange(10)
>>> C = A*B[..., np.newaxis]
>>> C.shape
>>> (10, 3)