Reputation: 11
Can anyone help me with this? I believe it's easy, but I don't know how to do it?
Create two matrices with elements 1,2,3,4,5
and 2,3,4,5,6
and matrix is
multiply.
I have this, but I don't know how to multiply them:
import numpy as np
a=np.arange(5).reshape(1,5)
b=np.arange(6).reshape(1,6)
print a
print b
Thanks for help.
Upvotes: 0
Views: 2943
Reputation: 122150
I think your problem is that your arrays aren't what you think they are:
>>> np.arange(5).reshape(1, 5)
array([[0, 1, 2, 3, 4]])
>>> np.arange(6).reshape(1, 6)
array([[0, 1, 2, 3, 4, 5]])
Instead, you probably want:
>>> np.arange(1, 6).reshape(1, 5)
array([[1, 2, 3, 4, 5]])
>>> np.arange(2, 7).reshape(1, 5)
array([[2, 3, 4, 5, 6]])
You can then multiply them directly:
>>> a = np.arange(1, 6).reshape(1, 5)
>>> b = np.arange(2, 7).reshape(1, 5)
>>> a*b
array([[ 2, 6, 12, 20, 30]])
Note that this is for element-wise multiplication. For the dot product, see leeladam
's answer.
Upvotes: 3