# 5 Best Ways to Rotate a Matrix in Python

Rate this post

π‘ Problem Formulation: Matrix rotation is a common operation in various computational problems, particularly in image processing and linear algebra. In this article, we address how to rotate a square matrix by 90 degrees in the counterclockwise direction. For example, if we have a matrix `[[1,2,3],[4,5,6],[7,8,9]]`, the desired output after a 90-degree rotation would be `[[3,6,9],[2,5,8],[1,4,7]]`. We will explore several methods to achieve this in Python.

## Method 1: Use Nested Loops

This traditional approach involves using nested loops to traverse the matrix, swapping elements in place to achieve the rotation. This method gives us the flexibility to rotate the matrix by any degree, although more complex than a simple 90-degree rotation.

Here’s an example:

```def rotate_matrix(matrix):
N = len(matrix)
for i in range(N // 2):
for j in range(i, N - i - 1):
temp = matrix[i][j]
matrix[i][j] = matrix[j][N - 1 - i]
matrix[j][N - 1 - i] = matrix[N - 1 - i][N - 1 - j]
matrix[N - 1 - i][N - 1 - j] = matrix[N - 1 - j][i]
matrix[N - 1 - j][i] = temp
return matrix

initial_matrix = [[1,2,3],[4,5,6],[7,8,9]]
rotated_matrix = rotate_matrix(initial_matrix)
print(rotated_matrix)```

Output:

`[[3, 6, 9], [2, 5, 8], [1, 4, 7]]`

This snippet defines a function `rotate_matrix` that takes a matrix and rotates it using temporary variables to hold values during swapping. It effectively walks around the ‘rings’ of the matrix, rotating the elements in layers from the outermost to the innermost.

## Method 2: Transpose and Reverse

A more pythonic and concise method involves transposing the matrix and then reversing each row. This method takes advantage of Python list comprehensions and built-in functions to achieve the rotation with fewer lines of code.

Here’s an example:

```def rotate_matrix(matrix):
return [list(reversed(col)) for col in zip(*matrix)]

initial_matrix = [[1,2,3],[4,5,6],[7,8,9]]
rotated_matrix = rotate_matrix(initial_matrix)
print(rotated_matrix)```

Output:

`[[3, 6, 9], [2, 5, 8], [1, 4, 7]]`

This code utilizes the built-in `zip` function which aggregates elements from each of the iterables (the original matrix rows) into new tuples, essentially transposing the matrix. Then, each tuple (now a row) is reversed and converted back to a list to complete the rotation.

## Method 3: List Slicing

Similar to Method 2, this approach also uses Python’s list comprehensions but approaches the transformation through list slicing. It’s a bit trickier to understand at a glance but is just as effective.

Here’s an example:

```def rotate_matrix(matrix):
return [list(row) for row in zip(*matrix[::-1])]

initial_matrix = [[1,2,3],[4,5,6],[7,8,9]]
rotated_matrix = rotate_matrix(initial_matrix)
print(rotated_matrix)```

Output:

`[[3, 6, 9], [2, 5, 8], [1, 4, 7]]`

This function first reverses the matrix with slicing `matrix[::-1]`, then uses `zip` to transpose it. The result is a 90-degree rotation, achieved through the matrix’s new rows which are tuples created by `zip` and converted back into lists.

## Method 4: Rotate In-place (for Square Matrices)

For strictly square matrices, this method efficiently performs the rotation in-place, which means that no additional memory is used to hold the rotated matrix. In-place rotation is usually more complex and requires careful indexing.

Here’s an example:

```def rotate_matrix(matrix):
N = len(matrix)
for i in range(N // 2):
for j in range(i, N - i - 1):
temp = matrix[i][j]
matrix[i][j] = matrix[N - 1 - j][i]
matrix[N - 1 - j][i] = matrix[N - 1 - i][N - 1 - j]
matrix[N - 1 - i][N - 1 - j] = matrix[j][N - 1 - i]
matrix[j][N - 1 - i] = temp
return matrix

initial_matrix = [[1,2,3],[4,5,6],[7,8,9]]
rotate_matrix(initial_matrix)
print(initial_matrix)```

Output:

`[[3, 6, 9], [2, 5, 8], [1, 4, 7]]`

This in-place version of the nested loops method operates directly on the input matrix without returning a new one. It’s more efficient in terms of space, as it requires no extra storage for the rotated matrix.

## Bonus One-Liner Method 5: Using NumPy

The NumPy library provides a clear and concise way to rotate matrices with its powerful array operations, ideal for large-scale numerical computations.

Here’s an example:

```import numpy as np

def rotate_matrix(matrix):
return np.rot90(matrix).tolist()

initial_matrix = np.array([[1,2,3],[4,5,6],[7,8,9]])
rotated_matrix = rotate_matrix(initial_matrix)
print(rotated_matrix)```

Output:

`[[3, 6, 9], [2, 5, 8], [1, 4, 7]]`

NumPy’s `rot90` function handles the rotation of an array by 90 degrees in the counterclockwise direction, simplifying this task to a one-liner. The result is converted to a list for consistency with the other methods.

## Summary/Discussion

• Method 1: Nested Loops. Offers flexibility for different rotation degrees. However, it’s verbose compared to Pythonic solutions.
• Method 2: Transpose and Reverse. It’s concise and leverages Python built-in functions. Not as intuitive for beginners but quite efficient for small to medium matrices.
• Method 3: List Slicing. Similar to Method 2 but uses slicing for the initial step. It’s compact but less readable at first glance.
• Method 4: In-place Rotation. Efficient memory usage since it modifies the matrix directly. Understandably more complex to get right.
• Bonus Method 5: Using NumPy. The most readable and concise method for those familiar with NumPy. Excellent for computation but adds a library dependency.