Matrix transpose java

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

matrix transpose java

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

Also read – matrix multiplication java

Transpose of a matrix in java can be obtained by interchanging all the rows and columns of matrix. In the below java program we are using for loop,

import java.util.Scanner;

public class MatrixTransposeInJava 
{
   public static void main(String[] args) 
   { 
      int a, b;
      System.out.println("Please enter rows and columns : ");
      Scanner sc = new Scanner(System.in);
      int row = sc.nextInt();
      int col = sc.nextInt();
      int arrNum[][] = new int[row][col];
      System.out.println("Please enter matrix elements : ");
      for(a = 0; a < row; a++)
      {
         for(b = 0; b < col; b++) 
         {
            arrNum[a][b] = sc.nextInt();
         }
      }
      System.out.println("Before transpose : ");
      for(a = 0; a < row; a++)
      {
         for(b = 0; b < col; b++)
         {
            System.out.print(arrNum[a][b] + " ");
         }
         System.out.println(" ");
      }
      System.out.println("After transpose : ");
      for(a = 0; a < col; a++)
      {
         for(b = 0; b < row; b++)
         {
            System.out.print(arrNum[b][a] + " ");
         }
         System.out.println(" ");
      }
      sc.close();
   }
}

Output :

Please enter rows and columns :
3 3
Please enter matrix elements :
2 4 6 8 1 3 5 7 9
Before transpose :
2 4 6
8 1 3
5 7 9
After transpose :
2 8 5
4 1 7
6 3 9


Here we are going to learn java program without using another matrix,

Also read – matrix addition and subtraction in java

import java.util.Scanner;

public class JavaTransposeMatrix 
{
   public static void main(String[] args) 
   { 
      Scanner sc = new Scanner(System.in); 
      int a, b, row, column, temp;
      System.out.println("Please enter number of rows : ");
      row = sc.nextInt();
      System.out.println("Please enter number of columns : ");
      column = sc.nextInt();

      int arrMatrix[][] = new int[row][column];

      System.out.println("Please enter elements of matrix : ") ;
      for(a = 0; a < row; a++)
      { 
         for(b = 0; b < column; b++)
         { 
            arrMatrix[a][b] = sc.nextInt();
         }
      }

      System.out.println("Elements of matrix : ") ;
      for(a = 0; a < row; a++)
      { 
         for(b = 0; b < column; b++)
         { 
            System.out.print(arrMatrix[a][b] + "\t");
         }
         System.out.println("");
      }

      // finding transpose
      for(a = 0; a < row; a++)
      { 
         for(b = 0; b < a; b++)
         {
            temp = arrMatrix[a][b];
            arrMatrix[a][b] = arrMatrix[b][a];
            arrMatrix[b][a] = temp;
         }
      }

      System.out.println("Transposed matrix : ") ;
      for(a = 0; a < row; a++)
      { 
         for(b = 0; b < column; b++)
         { 
            System.out.print(arrMatrix[a][b] + "\t");
         }
         System.out.println("");
      }
      sc.close();
   }
}

Output :

Please enter number of rows :
3
Please enter number of columns :
3
Please enter elements of matrix :
1 3 5 7 9 2 4 6 8
Elements of matrix :
1 3 5
7 9 2
4 6 8
Transposed matrix :
1 7 4
3 9 6
5 2 8

Related Posts