Example 69: Transpose a Matrix

Learn how to transpose a matrix.

Problem

Write a function to transpose a 3 x 3 matrix.

The function takes in two 2-D arrays as parameters, where the first array represents the original matrix, and in the second array, you need to store the transposed matrix.

Example

Input Output
1 2 3
4 5 6
7 8 9
1 4 7
2 5 8
3 6 9

Demonstration

The transpose of a matrix is an operator that flips a matrix over its diagonal. In other words, all rows become columns and all columns become rows, as shown in the illustration below:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.