Rotate Matrix in Python



Rotating a matrix in python, can be done by utilizing various approaches like Transpose and Reverse method which is commonly used in rotating a matrix by converting the rows to columns and columns to rows.

Common Approaches

Some of the common methods, we can use to rotate a matrix by 90 degress clockwise are as follows.

  • Layer by Layer Rotation

  • Temp Matrix Method

  • Transpose and reverse

2D Matrix

Let us consider an n x n 2D matrix, whic we have to rotate this matrix 90 degress clockwise.

1 5 7
9 6 3
2 1 3

Using Layer by Layer Rotation

This method includes rotating the matrix layer by layer, which doesn't require extra space for another matrix. The process of rotating a 2D matrix starts from the outermost layer and moving towards the inner layers. After it rotates the four corners of each layer.

Example

In the below example code the matrix is divided into four concentric layers, for each layer as follows:

  • Top row is moved to the right

  • Right column is moved to the bottom

  • Bottom row is moved to the left

  • Left column is moved to the top

Open Compiler
class Solution: def rotate(self, matrix): n = len(matrix) for i in range(n // 2): for j in range(i, n - i - 1): # Save the top element temp = matrix[i][j] # Move left to top matrix[i][j] = matrix[n - j - 1][i] # Move bottom to left matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1] # Move right to bottom matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1] # Move top to right matrix[j][n - i - 1] = temp return matrix ob1 = Solution() print(ob1.rotate([[1, 5, 7], [9, 6, 3], [2, 1, 3]]))

Input

[[1,5,7],[9,6,3],[2,1,3]]

Output

[[2, 9, 1], [1, 6, 5], [3, 3, 7]]

Rotated Matrix

2 9 1
1 6 5
3 3 7

Using Temp Matrix Method

As the name Implies this method creates a temporary matrix to store the rotated matrix. The original matrix is updated with rotated elements after creating the temporary matrix.

Example

In the below example code the 'temp_matrix' initialize a temporary matrix, which stores the rotated elements. populating the temporary matrix by extracting the elements from top to bottom and placing them as a row.

Open Compiler
class Solution: def rotate(self, matrix): temp_matrix = [] column = len(matrix) - 1 # Create the rotated matrix for column in range(len(matrix)): temp = [] for row in range(len(matrix) - 1, -1, -1): temp.append(matrix[row][column]) temp_matrix.append(temp) # Copy the rotated matrix back to the original matrix for i in range(len(matrix)): for j in range(len(matrix)): matrix[i][j] = temp_matrix[i][j] return matrix ob1 = Solution() print(ob1.rotate([[10, 15, 27], [11, 16, 8], [43, 17, 35]]))

Input

[[10, 15, 27], [11, 16, 8], [43, 17, 35]]

Output

[[43, 11, 10], [17, 16, 15], [35, 8, 27]]

Rotated Matrix

43 11 10
17 16 15
35 8 27

Using Transpose and reverse

This method is efficient and commonly used and the proces of rotating matrix involves two main steps are as follows below.

  • Transpose the matrix : Converting all the rows to columns and columns to rows of the matrix.

  • Reversing each row(elements) : In the next step Reversing the elements in each row to get our required rotation.

Example

In the below example code, the element at position '(i,j)' is swapped with the elements at position '(j,i)' and each row is reversed to get the required rotation of the matrix.

Open Compiler
class Solution: def rotate(self, matrix): n = len(matrix) # Transpose the matrix for i in range(n): for j in range(i, n): matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] # Reverse each row for i in range(n): matrix[i].reverse() return matrix ob1 = Solution() print(ob1.rotate([[1, 5, 7], [9, 6, 3], [2, 1, 3]]))

Input

[[1,5,7],[9,6,3],[2,1,3]]

Output

[[2, 9, 1], [1, 6, 5], [3, 3, 7]]

Rotated Matrix

2 9 1
1 6 5
3 3 7
Updated on: 2024-09-06T15:35:35+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements