Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tupleS = numpy.zeros( (N , 2) )
- tupleS[:,0] = S
- tupleS[:,1] = S
- product = A * tupleS
- >> A = numpy.array(range(10)).reshape(5, 2)
- >>> B = numpy.array(range(5))
- >>> B
- array([0, 1, 2, 3, 4])
- >>> A * B
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- ValueError: shape mismatch: objects cannot be broadcast to a single shape
- >>> B = B.reshape(5, 1)
- >>> B
- array([[0],
- [1],
- [2],
- [3],
- [4]])
- >>> A * B
- array([[ 0, 0],
- [ 2, 3],
- [ 8, 10],
- [18, 21],
- [32, 36]])
- product = A * S
- >>> S = np.arange(5)
- >>> S
- array([0, 1, 2, 3, 4])
- >>> A = np.arange(10).reshape((5,2))
- >>> A
- array([[0, 1],
- [2, 3],
- [4, 5],
- [6, 7],
- [8, 9]])
- >>> S[:,None]
- array([[0],
- [1],
- [2],
- [3],
- [4]])
- >>> A * S[:,None]
- array([[ 0, 0],
- [ 2, 3],
- [ 8, 10],
- [18, 21],
- [32, 36]])
- In []: N= 5
- In []: A= rand(N, 2)
- In []: A.shape
- Out[]: (5, 2)
- In []: S= rand(N)
- In []: S.shape
- Out[]: (5,)
- In []: A* S
- ------------------------------------------------------------
- Traceback (most recent call last):
- File "<ipython console>", line 1, in <module>
- ValueError: operands could not be broadcast together with shapes (5,2) (5)
- In []: A* S[:, None]
- Out[]:
- array([[ 0.54216549, 0.04964989],
- [ 0.41850647, 0.4197221 ],
- [ 0.03790031, 0.76744563],
- [ 0.29381325, 0.53480765],
- [ 0.0646535 , 0.07367852]])
- In []: expand_dims(S, 1).shape
- Out[]: (5, 1)
- In []: S= rand(N, 1)
- In []: S.shape
- Out[]: (5, 1)
- In []: A* S
- Out[]:
- array([[ 0.40421854, 0.03701712],
- [ 0.63891595, 0.64077179],
- [ 0.03117081, 0.63117954],
- [ 0.24695035, 0.44950641],
- [ 0.14191946, 0.16173008]])
- product = A * numpy.tile(S, (2,1)).T
- product = [d * S for d in A.T]
- product = numpy.array([d * S for d in A.T]).T
Add Comment
Please, Sign In to add comment