Python Program to Rotate Matrix Elements - GeeksforGeeks

28 Dec, 2021
Given a matrix, clockwise rotate elements in it.
Examples:
Input
1    2    3
4    5    6
7    8    9

Output:
4    1    2
7    5    3
8    9    6

For 4*4 matrix
Input:
1    2    3    4    
5    6    7    8
9    10   11   12
13   14   15   16

Output:
5    1    2    3
9    10   6    4
13   11   7    8
14   15   16   12
The idea is to use loops similar to the program for printing a matrix in spiral form. One by one rotate all rings of elements, starting from the outermost. To rotate a ring, we need to do following.     1) Move elements of top row.     2) Move elements of last column.     3) Move elements of bottom row.     4) Move elements of first column. Repeat above steps for inner ring while there is an inner ring.
Below is the implementation of above idea. Thanks to Gaurav Ahirwar for suggesting below solution. 
Python
# Python program to rotate a matrix  # Function to rotate a matrixdef rotateMatrix(mat):      if not len(mat):        return          """        top : starting row index        bottom : ending row index        left : starting column index        right : ending column index    """      top = 0    bottom = len(mat)-1      left = 0    right = len(mat[0])-1      while left < right and top < bottom:          # Store the first element of next row,        # this element will replace first element of        # current row        prev = mat[top+1][left]          # Move elements of top row one step right        for i in range(left, right+1):            curr = mat[top][i]            mat[top][i] = prev            prev = curr          top += 1          # Move elements of rightmost column one step downwards        for i in range(top, bottom+1):            curr = mat[i][right]            mat[i][right] = prev            prev = curr          right -= 1          # Move elements of bottom row one step left        for i in range(right, left-1, -1):            curr = mat[bottom][i]            mat[bottom][i] = prev            prev = curr          bottom -= 1          # Move elements of leftmost column one step upwards        for i in range(bottom, top-1, -1):            curr = mat[i][left]            mat[i][left] = prev            prev = curr          left += 1      return mat  # Utility Functiondef printMatrix(mat):    for row in mat:        print row    # Test case 1matrix =[             [1,  2,  3,  4 ],            [5,  6,  7,  8 ],            [9,  10, 11, 12 ],            [13, 14, 15, 16 ]          ]# Test case 2"""matrix =[            [1, 2, 3],            [4, 5, 6],            [7, 8, 9]        ]"""  matrix = rotateMatrix(matrix)# Print modified matrixprintMatrix(matrix)
Output: 
5 1 2 3
9 10 6 4
13 11 7 8
14 15 16 12
Please refer complete article on Rotate Matrix Elements for more details!
Amazon
rotation
Zoho
Matrix
Python
Python Programs
Zoho
Amazon
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Divide and Conquer | Set 5 (Strassen's Matrix Multiplication)
Count all possible paths from top left to bottom right of a mXn matrix
Program to multiply two matrices
Min Cost Path | DP-6
Printing all solutions in N-Queen Problem
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe