Algorithm


  1. Input:

    • Take the matrix as input.
  2. Initialize:

    • Create a new matrix to store the transpose.
  3. Transpose the Matrix:

    • Use nested loops to iterate through each element of the original matrix.
    • Swap the row and column indices of each element and store the result in the transpose matrix.
  4. Output:

    • Display the transpose matrix.

 

Code Examples

#1 Code Example- Program to Find Transpose of a Matrix

Code - Java Programming

public class Transpose {

    public static void main(String[] args) {
        int row = 2, column = 3;
        int[][] matrix = { {2, 3, 4}, {5, 6, 4} };

        // Display current matrix
        display(matrix);

        // Transpose the matrix
        int[][] transpose = new int[column][row];
        for(int i = 0; i  <  row; i++) {
            for (int j = 0; j  <  column; j++) {
                transpose[j][i] = matrix[i][j];
            }
        }

        // Display transposed matrix
        display(transpose);
    }

    public static void display(int[][] matrix) {
        System.out.println("The matrix is: ");
        for(int[] row : matrix) {
            for (int column : row) {
                System.out.print(column + "    ");
            }
            System.out.println();
        }
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
The matrix is:
2 3 4
5 6 4
The matrix is:
2 5
3 6
4 4
Advertisements

Demonstration


Java Programing Example to Find Transpose of a Matrix-DevsEnv