In this Java program, you’ll learn how to Find Transpose of a Matrix using the Java programming language.
How to Find Transpose of a Matrix using JAVA?
Example 1:
RUN CODE SNIPPETpublic class Main { public static void main(String[] args) { int row = 2, column = 3; int[][] matrix = { {1, 2, 3}, {4, 5, 6} }; display(matrix); int[][] transpose = new int[column][row]; for(int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { transpose[j][i] = matrix[i][j]; } } display(transpose); } public static void display(int[][] matrix) { System.out.println("The matrix is: "); for(int[] row : matrix) { for (int column : row) { System.out.print(column + " "); } System.out.println(); } } }
OUTPUT
The matrix is: 1 2 3 4 5 6 The matrix is: 1 4 2 5 3 6
For the transposed matrix we want to change the order of the matrix to 3*2, so we have transpose = int[column][row].
To transpose the matrix, we use transpose[j][i] = matrix[i][j]; by swapping the columns into row.