HomeJavaJava Program to Find Transpose of a Matrix

Java Program to Find Transpose of a Matrix

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 SNIPPET
public 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. 

Share:

Leave a Reply

You May Also Like

Java is a popular programming language that is used to develop a wide range of applications. If you are a...
  • Java
  • March 6, 2023
Java is a programming language and computing platform that is used to create various types of applications. It is used...
  • Java
  • March 6, 2023
In this post, you’ll learn how to download and install JUnit in Eclipse so that you can use in your...
  • Java
  • October 9, 2022