Matrix Transpose Java

Hey guys!! Welcome to flower brackets blog. Today you will learn matrix transpose java.

Here in this program elements entered by the user(input) will be in form of an array.

Also Read – Symmetric matrix program in java

Transpose of matrix is obtained by exchanging all the rows and columns of matrix with the help of for loop.

Matrix Transpose Java

Example: Matrix Transpose Java

Now let’s learn how to transpose an array in java. Here’s the example for transpose matrix java,

import java.util.Scanner;

public class MatrixTransposeInJava
{
   public static void main(String[] args)
   {
      int x, y, a, b;

      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter the number of rows and columns of matrix: ");
      x = sc.nextInt();
      y = sc.nextInt();

      int matrixTranspose[][] = new int[x][y];
      System.out.println("Please enter the elements of matrix : ");
      for(a = 0; a < x; a++)
      {
         for(b = 0; b < y; b++)
         {
            matrixTranspose[a][b] = sc.nextInt();
         }
      }

      int transpose[][] = new int[y][x];
      for(a = 0; a < x; a++)
      {
         for(b = 0; b < y; b++)
         {
            transpose[b][a] = matrixTranspose[a][b];
         }
      }

      System.out.println("Transposed Matrix : ");
      for(a = 0; a < y; a++)
      {
         for(b = 0; b < x; b++)
         {
            System.out.print(transpose[a][b] + "\t");
         }
         System.out.print("\n");
      }
      sc.close();
   }
}

Output :

Please enter the number of rows and columns of matrix: 2 3
Please enter the elements of matrix:
2 4 6
1 3 5
Transposed Matrix:
2    1
4    3
6    5


Transpose matrix java another way

Here we are going to learn transpose matrix java or how to print a matrix in java or transpose of a matrix in java,

public class JavaTransposeMatrix 
{
   public static void main(String[] args) 
   { 
      // create a matrix 
      int matrix[][] = {{2, 4, 6},{8, 1, 3},{5, 7, 9}}; 
 
      // creating transpose matrix java 
      int transpose[][] = new int[3][3]; 
 
      // transpose matrix java 
      for(int a = 0; a < 3; a++)
      { 
         for(int b = 0; b < 3; b++)
         { 
            transpose[a][b] = matrix[b][a]; 
         } 
      } 
 
      System.out.println("Before : Transpose matrix java"); 
      for(int a = 0; a < 3; a++)
      { 
         for(int b = 0; b < 3; b++)
         { 
            System.out.print(matrix[a][b] + " "); 
         } 
         System.out.println(); 
      }
 
      // transpose picture java
      // printing Transpose matrix java
      System.out.println("After : Transpose matrix java"); 
      for(int a = 0; a < 3; a++)
      { 
         for(int b = 0; b < 3; b++)
         { 
            System.out.print(transpose[a][b] + " "); 
         } 
         System.out.println(); 
      } 
   }
}

Output:

Before : Transpose matrix java
2 4 6
8 1 3
5 7 9
After : Transpose matrix java
2 8 5
4 1 7
6 3 9


Conclusion

That’s it guys. Here you learnt how to transpose an array in java or transpose matrix java. I hope you guys have understood the transpose of a matrix in java concept.

You guys can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

You May Also Like