Java Program to transpose matrix

Converting rows of a matrix into columns and columns of a matrix into row is called transpose of a matrix.

Let’s see a simple example to transpose a matrix of 3 rows and 3 columns.

  1. public class MatrixTransposeExample{  
  2. public static void main(String args[]){  
  3. //creating a matrix  
  4. int original[][]={{1,3,4},{2,4,3},{3,4,5}};    
  5.     
  6. //creating another matrix to store transpose of a matrix  
  7. int transpose[][]=new int[3][3];  //3 rows and 3 columns  
  8.     
  9. //Code to transpose a matrix  
  10. for(int i=0;i<3;i++){    
  11. for(int j=0;j<3;j++){    
  12. transpose[i][j]=original[j][i];  
  13. }    
  14. }    
  15.   
  16. System.out.println(“Printing Matrix without transpose:”);  
  17. for(int i=0;i<3;i++){    
  18. for(int j=0;j<3;j++){    
  19. System.out.print(original[i][j]+” “);    
  20. }    
  21. System.out.println();//new line    
  22. }    
  23. System.out.println(“Printing Matrix After Transpose:”);  
  24. for(int i=0;i<3;i++){    
  25. for(int j=0;j<3;j++){    
  26. System.out.print(transpose[i][j]+” “);    
  27. }    
  28. System.out.println();//new line    
  29. }    
  30. }}  

Output:

Printing Matrix without transpose:
1 3 4 
2 4 3 
3 4 5 
Printing Matrix After Transpose:
1 2 3 
3 4 4 
4 3 5 

Java Program to display transpose matrix

Let’s see another example where we are displaying transpose of a matrix. Here, we are not creating another matrix.

  1. public class MatrixTransposeExample2{  
  2. public static void main(String args[]){  
  3. //creating a matrix  
  4. int original[][]={{1,3,4},{2,4,3},{3,4,5}};    
  5.   
  6. System.out.println(“Printing Matrix without transpose:”);  
  7. for(int i=0;i<3;i++){    
  8. for(int j=0;j<3;j++){    
  9. System.out.print(original[i][j]+” “);    
  10. }    
  11. System.out.println();//new line    
  12. }    
  13. System.out.println(“Printing Matrix After Transpose:”);  
  14. for(int i=0;i<3;i++){    
  15. for(int j=0;j<3;j++){    
  16. System.out.print(original[j][i]+” “);    
  17. }    
  18. System.out.println();//new line    
  19. }    
  20. }}  

Output:

Printing Matrix without transpose:
1 3 4 
2 4 3 
3 4 5 
Printing Matrix After Transpose:
1 2 3 
3 4 4 
4 3 5 

Java program to transpose matrix (Another way)

You can also use a method where values of matrix are not predefined. Here, user has to put the values as input.

  1. import java.util.Scanner;  
  2. public class MatrixTransposeExample2   
  3. {  
  4.     public static void main(String args[])    
  5.     {  
  6.     int i, j;  
  7.     System.out.println(“Enter total rows and columns: “);  
  8.     Scanner s = new Scanner(System.in);  
  9.     int row = s.nextInt();  
  10.     int column = s.nextInt();  
  11.     int array[][] = new int[row][column];  
  12.     System.out.println(“Enter matrix:”);  
  13.     for(i = 0; i < row; i++)  
  14.     {  
  15.         for(j = 0; j < column; j++)   
  16.             {  
  17.             array[i][j] = s.nextInt();  
  18.             System.out.print(” “);  
  19.             }  
  20.     }  
  21.     System.out.println(“The above matrix before Transpose is “);  
  22.     for(i = 0; i < row; i++)  
  23.         {  
  24.             for(j = 0; j < column; j++)  
  25.             {  
  26.             System.out.print(array[i][j]+” “);  
  27.             }  
  28.             System.out.println(” “);  
  29.         }  
  30.     System.out.println(“The above matrix after Transpose is “);  
  31.     for(i = 0; i < column; i++)  
  32.         {  
  33.             for(j = 0; j < row; j++)  
  34.             {  
  35.                 System.out.print(array[j][i]+” “);  
  36.             }  
  37.             System.out.println(” “);  
  38.         }  
  39.     }  
  40. }  

Leave a Comment