Combining any two rows of a tensor

I have a tensor T with size (m, n) and I would like to concatenate any two rows. For example:

T = [[0,1], 
     [2,3], 
     [4,5]]
concat = [[0,1,2,3], [0,1,4,5], [2,3,0,1], [2,3,4,5], [4,5,0,1], [4,5,2,3]]

Is there any function able to do this? The only way that I have found is to use two nested for loops. Thanks.

from itertools import permutations
torch.stack([torch.cat(rows,0) for rows in permutations(T, 2)])

results in:

tensor([[0, 1, 2, 3],
        [0, 1, 4, 5],
        [2, 3, 0, 1],
        [2, 3, 4, 5],
        [4, 5, 0, 1],
        [4, 5, 2, 3]])
1 Like