Computer >> Computer tutorials >  >> Programming >> Java

Java Program to Find Transpose of a Matrix


In this article, we will understand how to find transpose of a matrix. The matrix has a row and column arrangement of its elements. A matrix with m rows and n columns can be called as m × n matrix. The transpose of a matrix is found by interchanging its rows into columns or columns into rows.

Below is a demonstration of the same −

Suppose our input is

The matrix is defined as:
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4

The desired output would be

The transpose of the matrix is:
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

Algorithm

Step 1 - START
Step 2 - Declare two integer matrices namely input_matrix and result_matrix.
Step 3 - Define the values.
Step 4 - Iterate over each element of the matrix using two for-loops, assign the element at [i][j] position of the matrix to the [j][i] position of the result_matrix.
Step 5 - Display the result_matrix.
Step 6 - Stop

Example 1

Here, we bind all the operations together under the ‘main’ function.

public class MatrixTranspose {
   static final int matrix_size = 4;
   public static void main (String[] args) {
      int input_matrix[][] = {
         {1, 1, 1, 1},
         {2, 2, 2, 2},
         {3, 3, 3, 3},
         {4, 4, 4, 4}
      };
      System.out.println("The matrix is defined as: \n");
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            System.out.print(input_matrix[i][j] + " ");
         }
         System.out.println();
      }
      int result_matrix[][] = new int[matrix_size][matrix_size];
      for (int i = 0; i < matrix_size; i++)
         for (int j = 0; j < matrix_size; j++)
            result_matrix[i][j] = input_matrix[j][i];
      System.out.println("\nThe transpose of the matrix is: ");
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            System.out.print(result_matrix[i][j] + " ");
         }
         System.out.println();
      }
   }
}

Output

The matrix is defined as:

1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4

The transpose of the matrix is:
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

Example 2

Here, we encapsulate the operations into functions exhibiting object oriented programming.

public class MatrixTranspose {
   static final int matrix_size = 4;
   static void transpose(int input_matrix[][]) {
      int result_matrix[][] = new int[matrix_size][matrix_size];
      for (int i = 0; i < matrix_size; i++)
         for (int j = 0; j < matrix_size; j++)
            result_matrix[i][j] = input_matrix[j][i];
      System.out.println("\nThe transpose of the matrix is: ");
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            System.out.print(result_matrix[i][j] + " ");
         }
         System.out.println();
      }
   }
   public static void main (String[] args) {
      int input_matrix[][] = {
         {1, 1, 1, 1},
         {2, 2, 2, 2},
         {3, 3, 3, 3},
         {4, 4, 4, 4}
      };

      System.out.println("The matrix is defined as: \n");
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            System.out.print(input_matrix[i][j] + " ");
         }
         System.out.println();
      }
      transpose(input_matrix);
   }
}

Output

The matrix is defined as:
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4

The transpose of the matrix is:
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4