srush / Tensor-Puzzles

Solve puzzles. Improve your pytorch.
MIT License
2.96k stars 242 forks source link

Hints on 10 (roll) #21

Closed kshitijsachan closed 11 months ago

kshitijsachan commented 11 months ago

I'm not sure how to solve roll. I think I should make a permutation matrix, but I don't know how to make the full permutation matrix in one line? Would appreciate a hint.

srush commented 11 months ago

There are two ways to do this. One is to create a permutation matrix as you say. The other is to use advanced indexing, and just use the fact that arr[ind_arr] will work even if ind_arr is a tensor.

kshitijsachan commented 11 months ago

Ah advanced indexing, of course! Thanks :)