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 transpose matrix is one of the common interview question in java.

Also read – java program for matrix multiplication

Transpose means converting rows of matrix into columns and columns of matrix into row.

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 find transpose of a matrix.

import java.util.Scanner;
public class MatrixTransposeInJava
{
   public static void main(String[] args)
   {
      int[][] arrGiven = {{2,4,6},{8,1,3},{5,7,9}};	    
      // another matrix to store transpose of matrix  
      int[][] arrTranspose = new int[3][3];	    
      // transpose matrix code  
      for(int a = 0; a < 3; a++)
      {    
         for(int b = 0; b < 3; b++)
         {    
            arrTranspose[a][b] = arrGiven[b][a];  
         } 
      }	  
      System.out.println("Before matrix transpose: ");  
      for(int a = 0; a < 3; a++)
      {    
         for(int b = 0; b < 3; b++)
         {    
            System.out.print(arrGiven[a][b] + " ");    
         } 
         System.out.println();
      }    
      System.out.println("After matrix transpose: ");  
      for(int a = 0; a < 3; a++)
      {    
         for(int b = 0; b < 3; b++)
         {    
            System.out.print(arrTranspose[a][b] + " ");    
         } 
         System.out.println();
      }
   }
}


Output:

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


java program to 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 Print3x3Matrix
{
   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.println(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


Transpose of a matrix using BufferedReader

Now let’s learn transpose of a matrix in java using BufferedReader.

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

In the below java program ‘printTranspose()’ function print the contents of transpose matrix.

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

public class TransposeUsingFunction
{
   public static void main(String[] args)
   {
      int row = 3, col = 3;
      int[][] matrixGiven = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
      // printing given matrix
      System.out.println("Given matrix is: ");
      printTranspose(matrixGiven);
      // transpose matrix
      int[][] matrixTranspose = new int[col][row];
      for(int a = 0; a < row; a++)
      {
         for(int b = 0; b < col; b++)
         {
            matrixTranspose[b][a] = matrixGiven[a][b];
         }
      }
      // printing transposed matrix
      System.out.println("Transpose matrix is: ");
      printTranspose(matrixTranspose);
   }
   static void printTranspose(int[][] mat)
   {
      for(int[] row : mat)
      {
         for(int col : row)
         {
            System.out.print(col + " ");
         }
         System.out.println();
      }
   }
}


Output:

Given matrix is:
1 2 3
4 5 6
7 8 9
Transpose matrix is:
1 4 7
2 5 8
3 6 9


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