The transpose of a matrix is obtained by converting its rows into columns and columns into rows. In other words, for a matrix A, the element at A[i][j] moves to A[j][i].
Illustration
Input: [ [ 1 , 2 , 3 ] ,
[ 4 , 5 , 6 ] ,
[ 7 , 8 , 9 ] ]
Output: [ [ 1 , 4 , 7 ] ,
[ 2 , 5 , 8 ] ,
[ 3 , 6 , 9 ] ]
There are two cases which we will face while transposing the Matrix as mentioned below:
- Rectangular Matrix: Number of rows ≠ number of columns
- Square Matrix: Number of rows = number of columns
1. Transpose of a Rectangular Matrix
For a rectangular matrix, we need to create a new matrix because the number of rows and columns changes after transposition.
Approach
- Create a result matrix with columns rows and rows columns.
- Traverse the original matrix.
- Store A[i][j] at B[j][i].
- Print the transpose matrix.
public class GFG {
static void transpose(int[][] A, int[][] B) {
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A[i].length; j++) {
B[j][i] = A[i][j];
}
}
}
public static void main(String[] args) {
int[][] A = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int rows = A.length;
int cols = A[0].length;
// Transposed matrix has cols rows and rows columns
int[][] B = new int[cols][rows];
transpose(A, B);
for (int[] row : B) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}
}
}
Output
1 5 9 2 6 10 3 7 11 4 8 12
Explanation:
- Original matrix has 3 rows and 4 columns.
- Transposed matrix has 4 rows and 3 columns.
- B[j][i] = A[i][j] swaps the row and column positions.
2. Transpose of a Square Matrix Using Extra Space
For a square matrix, we can also create a separate matrix to store the transpose.
public class GFG {
static void transpose(int[][] A, int[][] B) {
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A.length; j++) {
B[j][i] = A[i][j];
}
}
}
public static void main(String[] args) {
int[][] A = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int n = A.length;
int[][] B = new int[n][n];
transpose(A, B);
for (int[] row : B) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}
}
}
Output
1 4 7 2 5 8 3 6 9
3. In-Place Transpose of a Square Matrix
A square matrix can be transposed without creating another matrix. We simply swap the elements above the main diagonal with their corresponding elements below it.
Approach
- Start from the first row.
- Compare each element with the corresponding element below the main diagonal.
- Swap A[i][j] and A[j][i].
- Start j from i + 1 to avoid swapping the same elements twice.
public class GFG {
static void transpose(int[][] A) {
for (int i = 0; i < A.length; i++) {
for (int j = i + 1; j < A.length; j++) {
int temp = A[i][j];
A[i][j] = A[j][i];
A[j][i] = temp;
}
}
}
public static void main(String[] args) {
int[][] A = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
transpose(A);
for (int[] row : A) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}
}
}
Output
1 4 7 2 5 8 3 6 9
Explanation:
- A[i][j] is swapped with A[j][i].
- The main diagonal elements remain unchanged.
- Only the upper triangular part is traversed.
- No additional matrix is required.