Java program to find transpose of a matrix

Let’s learn java program to find transpose of a matrix.

Java program to find transpose of a matrix

Java program to display transpose matrix is one of the common interview question in java programming.

To print the transpose of a given matrix in java user enters number of rows and columns using nextInt() method of Scanner class.

Also read – java program for matrix multiplication

These user entered rows and columns are stored in interger variables ‘row’ and ‘col’. In the next step user enters matrix elements.

Transpose of a matrix in java can be performed by converting columns of matrix to row and rows of matrix to columns.

For example:

3x3 matrix

a11    a12    a13
a21    a22    a23
a31    a32    a33

transposed 3x3 matrix

a11    a21    a31
a12    a22    a32
a13    a23    a33

Let’s see java program to display transpose a matrix or transpose of a matrix in java using scanner,

import java.util.Scanner;

public class MatrixTransposeInJava 
{
   public static void main(String[] args) 
   { 
      int a, b;
      System.out.println("Please enter rows and columns : ");
      // transpose of a matrix in java using scanner
      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:

java program to find transpose of a matrix

Print 3×3 matrix

Let’s learn java program to print 3×3 matrix. Here’s 3×3 matrix in java example,

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MatrixTransposeDemo 
{
   public static void main(String[] args) throws IOException 
   {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int[][] inputNumber = new int[3][3];
      int a, b;
      String strMatrix;
      System.out.println("Please enter elements to print 3x3 matrix: ");
      for(a = 0; a <= 2; a++)
      {
         for(b = 0; b <= 2; b++)
         {
            strMatrix = br.readLine();
            inputNumber[a][b]=Integer.parseInt(strMatrix);
         }
      }
      System.out.println("Elements in 3x3 matrix are: ");
      System.out.println("");
      for(a = 0; a <= 2; a++)
      {
         for(b = 0; b <= 2; b++)
         {
            System.out.print(inputNumber[a][b] + "\t");
         }
         System.out.println();
      }
   }
}


Output:

Please enter elements to print 3×3 matrix:
2
4
6
8
1
3
5
7
9
Elements in 3×3 matrix are:

2 4 6
8 1 3
5 7 9


java program to find trace of a matrix

Trace of a matrix is nothing but sum of diagonal elements. For example consider square matrix m x m,

1 2 3
4 5 6
7 8 9

Trace of a matrix is: 1 + 5 + 9 = 15

Here we are going to learn java program to find trace of a matrix,

import java.util.Scanner;

public class TraceMatrixDemo 
{
   public static void main(String[] args) 
   {
      int[][] arrInput = new int[10][10];
      int a, b;
      double total = 0;
      System.out.println("Please enter total rows and columns: ");
      Scanner sc = new Scanner(System.in);
      int row = sc.nextInt();
      int column = sc.nextInt();
      System.out.println("Please enter matrix: ");
      for(a = 0; a < row; a++)
      {
         for(b = 0; b < column; b++) 
         {
            arrInput[a][b] = sc.nextInt();
            System.out.print(" ");
         }
      }
      System.out.println("Entered matrix is: ");
      for(a = 0; a < row; a++)
      {
         for(b = 0; b < column; b++)
         {
            System.out.print(arrInput[a][b] + " ");
         }
         System.out.println(" ");
      }
      System.out.println("Trace of a matrix: ");
      for(a = 0; a < row; a++)
      {  
         for(b = 0; b < column; b++)
         {
            if(a == b)
            {
               total = total + (arrInput[a][b]);
            }
         }
      }
      System.out.println(total);
      sc.close();
   }
}


Output:

Please enter total rows and columns:
3
3
Please enter matrix:
2
4
6
8
1
3
5
7
9
Entered matrix is:
2 4 6
8 1 3
5 7 9
Trace of a matrix:
12.0


Using bufferedreader

Now let’s learn transpose of a matrix in java using bufferedreader. Here’s java program to transpose a matrix,

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TransposeMatrixDemo 
{
   public static void main(String[] args) throws IOException 
   {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Please enter number of rows: ");
      int row = Integer.parseInt(br.readLine());
      System.out.print("Please enter number of columns: ");
      int col = Integer.parseInt(br.readLine());
      int[][] arrMatrix = new int[row][col];
      int[][] arrTranspose = new int[row][col];
      System.out.println("Please enter elements of matrix: ");
      for(int a = 0; a < row; a++) 
      {
         for(int b = 0; b < col; b++) 
         {
            arrMatrix[a][b] = Integer.parseInt(br.readLine());
         }
      }
      System.out.println("Given Matrix\n");
      for(int a = 0; a < row; a++) 
      {
         for(int b = 0; b < col; b++) 
         {
            System.out.print(arrMatrix[a][b] + " ");
         }
         System.out.print("\n");
      }
      for(int a = 0; a < row; a++) 
      {
         for(int b = 0; b < col; b++) 
         {
            arrTranspose[b][a] = arrMatrix[a][b];
         }
      }
      System.out.println("Transpose matrix in java \n");
      for(int a = 0; a < col; a++) 
      {
         for(int b = 0; b < row; b++) 
         {
            System.out.print(arrTranspose[a][b] + " ");
         }
         System.out.print("\n");
      }
   }
}


Output:

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

1 2 3
4 5 6
7 8 9
Transpose matrix in java

1 4 7
2 5 8
3 6 9


transpose of matrix in java using function

Here’s the program for transpose of matrix in java using function,

public class TransposeUsingFunction
{
   static final int num = 4;

   public static void main(String[] args)
   {
      int[][] matrix1 = { {5, 5, 5, 5},
                          {6, 6, 6, 6},
                          {7, 7, 7, 7},
                          {8, 8, 8, 8}};
      int[][] matrix2 = new int[num][num];
      functionTranspose(matrix1, matrix2);
      System.out.print("transpose of matrix using function: \n");
      for(int a = 0; a < num; a++)
      {
         for(int b = 0; b < num; b++)
            System.out.print(matrix2[a][b] + " ");
         System.out.print("\n");
      }
   }

   static void functionTranspose(int mat1[][], int mat2[][])
   {
      int a, b;
      for(a = 0; a < num; a++)
         for(b = 0; b < num; b++)
            mat2[a][b] = mat1[b][a];
   }
}


Output:

transpose of matrix using function:
5 6 7 8
5 6 7 8
5 6 7 8
5 6 7 8


Also read – multiplication of a matrix in java


transpose of a matrix in java without using second matrix

Let’s learn java program for transpose of a matrix in java without using second matrix,

import java.util.Scanner;

public class WithoutSecondMatrix
{
   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 the number of columns: ");
      column = sc.nextInt();
      int[][] matrix = new int[row][column];
      System.out.println("Please enter elements of matrix: ") ;
      for(a = 0; a < row; a++)
      {
         for(b = 0; b < column; b++)
         {
            matrix[a][b] = sc.nextInt();
         }
      }
      System.out.println("Elements of the matrix: ") ;
      for(a = 0; a < row; a++)
      {
         for(b = 0; b < column; b++)
         {
            System.out.print(matrix[a][b] + "\t");
         }
         System.out.println("");
      }
      // transpose of a matrix
      for(a = 0; a < row; a++)
      {
         for(b = 0; b < a; b++)
         {
            temp = matrix[a][b];
            matrix[a][b] = matrix[b][a];
            matrix[b][a] = temp;
         }
      }
      System.out.println("transpose of a matrix without using second matrix: ") ;
      for(a = 0; a < row; a++)
      {
         for(b = 0; b < column; b++)
         {
            System.out.print(matrix[a][b] + "\t");
         }
         System.out.println("");
      }
      sc.close();
   }
}


Output:

Please enter number of rows: 3
Please enter the number of columns: 3
Please enter elements of matrix:
1 3 5
7 9 2
4 6 8
Elements of the matrix:
1 3 5
7 9 2
4 6 8
transpose of a matrix without using second matrix:
1 7 4
3 9 6
5 2 8