GeeksforGeeks App
Open App
Browser
Continue

# How to Shuffle Columns or Rows of Matrix in PyTorch?

In this article, we will see how to shuffle columns and rows of a matrix in PyTorch.

## Column Shuffling:

Row and Column index starts with 0 so by specifying column indices in the order, we will shuffle columns. Here we will change the column positions.

Syntax: t1[torch.tensor([row_indices])][:,torch.tensor([column_indices])]

where,

• row_indices and column_indices are the index positions in which they are shuffled based on the positions.
• t1 represents tensor which of 2 dimensional.

Example 1:

In this example, we are creating a tensor named t1, which is of 2 dimensions of 3 rows, and 3 columns are created. After that, we are shuffling columns in such a way that we are moving column elements from the first position to the third position and the third position to the first position.

## Python3

 `# importing torch``import` `torch`` ` `# create tensor``t1 ``=` `torch.tensor([[``1``, ``2``, ``3``],``                   ``[``5``, ``6``, ``7``],``                   ``[``9``, ``10``, ``11``]])`` ` `# printing the tensor``print``(t1)`` ` `print``()`` ` `# shuffle columns - first position ``# to third position and``# third position to first position``print``(t1[torch.tensor([``0``, ``1``, ``2``])][:, torch.tensor([``2``, ``1``, ``0``])])`

Output:

```tensor([[ 1,  2,  3],
[ 5,  6,  7],
[ 9, 10, 11]])

tensor([[ 3,  2,  1],
[ 7,  6,  5],
[11, 10,  9]])```

Example 2:

In this example, we are creating a tensor named t1, which is of 2 dimensions of 3 rows and 3 columns. After that we are shuffling columns in such a way that second position elements are moved to the third position, third position elements are moved to a first position and first position elements are moved to the second position.

## Python3

 `# importing torch``import` `torch`` ` `# create tensor``t1 ``=` `torch.tensor([[``1``, ``2``, ``3``],``                   ``[``5``, ``6``, ``7``],``                   ``[``9``, ``10``, ``11``]])`` ` `# printing the tensor``print``(t1)`` ` `print``()`` ` `# shuffle columns - second  position``# to third position ,``# third position to first position``# and first position to second position``print``(t1[torch.tensor([``0``, ``1``, ``2``])][:, torch.tensor([``1``, ``2``, ``0``])])`

Output:

```tensor([[ 1,  2,  3],
[ 5,  6,  7],
[ 9, 10, 11]])

tensor([[ 2,  3,  1],
[ 6,  7,  5],
[10, 11,  9]])```

## Row Shuffling:

Row and Column index starts with 0 so by specifying column indices in the order, we will shuffle columns. Here we will change the row positions.

Syntax:t1[torch.tensor([row_indices])][:,torch.tensor([column_indices])]

where,

• row_indices and column_indices are the index positions in which they are shuffled based on the positions.
• t1 represents tensor which of 2 dimensional.

Example 1:

In this example, we are creating a tensor named t1, which is of 2 dimensions of 3 rows and 3 columns. After that, we are shuffling rows from the first position to the third position and from the third position to the first position.

## Python3

 `# importing torch``import` `torch`` ` `# create tensor``t1 ``=` `torch.tensor([[``1``, ``2``, ``3``],``                   ``[``5``, ``6``, ``7``],``                   ``[``9``, ``10``, ``11``]])`` ` `# printing the tensor``print``(t1)`` ` `print``()`` ` `# shuffle rows   - first position to third position and``# third position to first position``print``(t1[torch.tensor([``2``, ``1``, ``0``])][:, torch.tensor([``0``, ``1``, ``2``])])`

Output:

```tensor([[ 1,  2,  3],
[ 5,  6,  7],
[ 9, 10, 11]])

tensor([[ 9, 10, 11],
[ 5,  6,  7],
[ 1,  2,  3]])```

Example 2:

In this example, we are creating a tensor named t1, which is of 2 dimensions of 3 rows and 3 columns. After that we are shuffling the rows in such a way that second position elements are moved to the third position, third position elements are moved to the first position and first position elements are moved to the second position.

## Python3

 `# importing torch``import` `torch`` ` `# create tensor``t1 ``=` `torch.tensor([[``1``, ``2``, ``3``],``                   ``[``5``, ``6``, ``7``],``                   ``[``9``, ``10``, ``11``]])`` ` `# printing the tensor``print``(t1)`` ` `print``()`` ` `# shuffle rows   - second  position to third position ,``# third position to first position and first position``# to second position``print``(t1[torch.tensor([``1``, ``2``, ``0``])][:, torch.tensor([``0``, ``1``, ``2``])])`

Output:

```tensor([[ 1,  2,  3],
[ 5,  6,  7],
[ 9, 10, 11]])

tensor([[ 5,  6,  7],
[ 9, 10, 11],
[ 1,  2,  3]])```

My Personal Notes arrow_drop_up