Guest User

Untitled

a guest
Apr 26th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. # The matrix to be modified
  2. a = np.zeros((2,10))
  3. # Indices array of size N
  4. indices = np.array([1,4])
  5. # Indexing, the result must be
  6. a = a[at indices per row]
  7. print a
  8.  
  9. [[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
  10. [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]]
  11.  
  12. >>> a[np.arange(2),indices] = 1
  13. >>> a
  14. array([[ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
  15. [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]])
  16.  
  17. >>> a[np.where(indices)+(indices,)] = 1
  18. >>> a
  19. array([[ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
  20. [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]])
  21.  
  22. >>> np.eye(a.shape[1])[indices]
  23. array([[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
  24. [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]])
Add Comment
Please, Sign In to add comment