create tensor in specific indexes without loops
I have a tensor t1 (with shape (2*n, 2*n), and I need to create tensor t2 (with shape (2*n)) with the values of t1 at [i,(i+n) mod 2n] for each row i.
For example, given:
t1 = torch.tensor([[1, 2, 3, 4],
                   [5, 6, 7, 8],
                   [9 ,10,11,12],
                   [13,14,15,16]])

Here n=2.
t2 has to be [3,8,9,14].
I have this code:
t2 = torch.tensor([t1[i,(i+n)%(2*n)] for i in range(2*n)])

but Im searching a way without loops.

You could create the list of indices beforehand with a loop then pick the values from t1 with torch.gather without having to loop over them yourself:
&gt;&gt;&gt; index = torch.tensor([[(i+n)%(2*n)] for i in range(2*n)])

&gt;&gt;&gt; torch.gather(t1, 1, index).flatten()
tensor([ 3,  8,  9, 14])

Alternatively, you can use torch.roll to shift by n an (2*n, 2*n) identity matrix, multiply by t1 to get a mask and apply that to t1 with torch.masked_select:
&gt;&gt;&gt; mask = torch.eye(2*n).roll(n, 1).bool()
tensor([[False, False,  True, False],
        [False, False, False,  True],
        [ True, False, False, False],
        [False,  True, False, False]])

&gt;&gt;&gt; torch.masked_select(t1, mask)
tensor([ 3,  8,  9, 14])

