Matrix Rotation and Matrix Transpose

Problem Definition – Matrix Rotation (by 90, 180, 270 degrees)

This is a very famous interview question and has been asked numerous times. We are trying to solve the problem of matrix rotations where the inputs are as follows:

  • A matrix of dimension M * N
  • A number from the set (90, 180, ,270) by which we need to rotate the matrix.

For simplicity we will consider a matrix to be a 2 dimensional array of integers.

What does it mean to rotate a matrix?

This can only be explained by nice diagrams. Just for a formal definition, I would say that matrix rotation is a structural re-arrangement of the rows and columns of the matrix and it is achieved by a sequence of operations on the matrix as a whole. Depending on the degrees by which we want to rotate, the permutations of the operations may vary.

Prerequisites – Matrix Rotations

A necessary prerequisite for the matrix rotations is to have knowledge about matrix transpose. So, let us first talk about transpose.

A transpose of a matrix A(M * N) is represented by AT and the dimensions of AT is N * M.
Here is an image to demonstrate the transpose of a given matrix.
matrix rotation - transpose_new

The red boundaries (rows) in the matrix A becomes the column in AT and the blue boundaries (columns) in matrix B becomes the row in BT.

In simple words, if we can exchange each column with each of the rows in order (1st column with first row, second column with second row and so on..) then at the end of the iteration we will get the transpose of the matrix.

Horizontal Reflection

This precisely means exchanging one row with the other such that the first row becomes the last, the second row becomes the second last and so on..

For a matrix of dimension M * N there are M rows and N columns and horizontal reflection means Row M is exchanged with Row 1, Row M-1 is exchange with Row 2 so on. If the value of M is odd then the position of row 1 + M/2 remains intact and it is not changed, that is why it is also called horizontal reflection through middle row.

Here is an example of horizontal reflection.
matrix rotation - reflect_horizontally

Vertical Reflection

This precisely means exchanging one column with the other such that the first column becomes the last, the second column becomes the second last and so on..

For a matrix of dimension M * N there are M rows and N columns and vertical reflection means Column N is exchanged with Column 1, Column N-1 is exchange with Column 2 so on. If the value of N is odd then the position of Column 1 + N/2 remains intact and it is not changed, that is why it is also called vertical reflection through the middle column.

Here is an example of vertical reflection.
matrix rotation - reflect_vertically

Steps for Matrix Rotation – 90 degrees
  • Take transpose of the matrix.
  • Take reflection of the transpose against the horizontal axis.

matrix rotation - 90degrees

Steps for Matrix Rotation – 180 degrees
  • Take reflection of the matrix against the horizontal axis.
  • Take reflection of the resultant matrix against the vertical axis.

matrix rotation - 180degress

Steps for Matrix Rotation – 270 degrees
  • Take transpose of the matrix.
  • Take reflection of the transpose against the vertical axis.

matrix rotation - 270degress

Source Code – Matrix Rotation

Don’t forget to subscribe to TechieMe to get updates on latest posts.
Here is a Matrix Rotation WIKI for the topic