Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # The matrix to be modified
- a = np.zeros((2,10))
- # Indices array of size N
- indices = np.array([1,4])
- # Indexing, the result must be
- a = a[at indices per row]
- print a
- [[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]]
- >>> a[np.arange(2),indices] = 1
- >>> a
- array([[ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
- [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]])
- >>> a[np.where(indices)+(indices,)] = 1
- >>> a
- array([[ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
- [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]])
- >>> np.eye(a.shape[1])[indices]
- array([[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]])
Add Comment
Please, Sign In to add comment