0

I have a tensor x, that looks like this:

x = tensor([  1,  2,  3,  4,  5],
           [  6,  7,  8,  9, 10]
           [ 11, 12, 13, 14, 15])

I'm trying to switch the first two and last two numbers of each tensor, like this:

x = tensor([  4,  5,  3,  1,  2],
           [  9, 10,  8,  6,  7],
           [ 14, 15, 13, 11, 12])

How could I do this with torch.roll()? How would I switch 3 instead of 1?

1
  • What do you mean by switch 3 instead of 1? Commented Jan 20, 2022 at 9:54

1 Answer 1

1

Not sure if that can be done with torch.roll alone... However, you can expect the desired result by using a temporary tensor and a pair assignment:

>>> x = torch.arange(1, 16).reshape(3,-1)
tensor([[ 1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10],
        [11, 12, 13, 14, 15]])

>>> tmp = x.clone()

# swap the two sets of columns
>>> x[:,:2], x[:,-2:] = tmp[:,-2:], tmp[:,:2]

Such that tensor x has been mutated as:

>>> x
tensor([[ 4,  5,  3,  1,  2],
        [ 9, 10,  8,  6,  7],
        [14, 15, 13, 11, 12]])

You can pull off this operation with torch.roll and some indexing:

>>> x = torch.arange(1, 21).reshape(4,-1)
tensor([[ 1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10],
        [11, 12, 13, 14, 15],
        [16, 17, 18, 19, 20]])

>>> rolled = x.roll(-2,0)
tensor([[11, 12, 13, 14, 15],
        [16, 17, 18, 19, 20],
        [ 1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10]])

# overwrite columns [1,-1[ from rolled with those from x
>>> rolled[:, 1:-1] = x[:, 1:-1]

Such that at this end you get:

>>> rolled
tensor([[11,  2,  3,  4, 15],
        [16,  7,  8,  9, 20],
        [ 1, 12, 13, 14,  5],
        [ 6, 17, 18, 19, 10]])
Sign up to request clarification or add additional context in comments.

2 Comments

Nice, I didn't know about that notation. Could I use the same notation to do the same in the y-direction? This would mean that the numbers would be moving cross element. So the input should be: tensor([ [1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], [16,17,18,19,20] ]) And the output: tensor([ [11,2,3,4,15], [16,7,8,9,20], [1,12,13,14,5], [6,17,18,19,10] ])
@DarrelGulseth This actually would require torch.roll this time around. Please look at my edit above for a full walkthrough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.