Disclosure: This article may contain affiliate links. When you purchase, we may earn a small commission.

How to Transpose a Matrix in Java? Example Tutorial

Hello guys, if you are wondering how to transpose a matrix in Java then you have come to the right place. Matrix related coding problems are great way to learn multi-dimensional array and nested loop and they are good programming exercise for beginners. In the past, I have taught you how to multiply matrices in Java and how to add/subtract matrices in Java, and in this article, I will show you how to create transpose of a given matrix in Java, but before that let's first understand what is transpose of a matrix means and how do you transpose a matrix in maths? Well, a transpose of a matrix is nothing but a matrix whose rows and columns are reversed. 

For example, if you have a 2x3 matrix which has 2 rows and 3 columns then you can create a transpose matrix by turning all the rows into columns and all the columns into row. So the transpose of that matrix will be a 3x2 matrix, I mean a matrix with 3 rows and 2 columns. 

But, if you matrix are symmetric i.e. both rows and columns are same length like 2x 2, 3x3 or 4x4 then both actual matrix and transpose of the matrix will have the same number of rows and columns. 

In Java, matrix is represented by multi-dimensional array. For example a 2 x 3 matrix is a two dimensional array where each array has 3 elements. In order to transpose a matrix in Java, you need to create a new array where number of column becomes rows and number of rows become number of columns. 

After that you just loop through the original matrix and start copying elements from row into columns and vice-versa.  This will be more clear when you will see the example of transposing a matrix in our Java program.




Java program to create transpose of matrix

Here is a sample code in Java to transpose a given Matrix.  This program takes input from user to create original matrix and then creates a transpose matrix and print into console. This code can be used to check if a matrix symmetric or not, just compare the matrix with its transpose if they are same then it's symmetric otherwise non symmetric, also it's also useful for calculating orthogonally of a matrix. 

How to Transpose a Matrix in Java? Example Tutorial

And, here is our complete Java program you can try in your IDE or run from command line to see transposing your matrix in action. 


import java.util.Random;
import java.util.Scanner;

/**
* Java program to find transpose of a matrix of any order. matrix exercises are
* good way to learn two dimensional and multi-dimensional array in Java.
*
* @author Javin Paul
*/
public class Testing {

    public static void main(String args[]) {
        int row = 0;
        int column = 0;
        int n, c, d;

        Scanner console = new Scanner(System.in);
        System.out.println("Enter the number of rows and columns in matrix");
        row = console.nextInt();
        column = console.nextInt();

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

        System.out.println("Enter the elements of matrix");

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                matrix[i][j] = console.nextInt();
            }
        }

        System.out.println("Transpose of entered matrix ");

        int transpose[][] = transpose(matrix);
        for (int i = 0; i < column; i++) {
            for (int j = 0; j < row; j++) {
                System.out.printf("%s ", transpose[j][i]);
            }
            System.out.print("\n");
        }

    }

    /**
     * @return transpose of given matrix in Java
     */
    public static int[][] transpose(int[][] matrix) {
        int rows = matrix.length;
        int columns = matrix[0].length;
        int transpose[][] = new int[columns][rows];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                transpose[j][i] = matrix[i][j];
            }
        }
        return matrix;
    }

}


Output:
Enter the number of rows and columns in matrix
2
3
Enter the elements of matrix
10 12 16
23 26 54
Transpose of entered matrix
10 23
12 26
16 54


That's all about how to transpose a matrix in Java. This is an interesting Java programming exercise to learn about array data structure, particularly two dimensional array and nested loop. By solving matrix related coding problem you slowly become better at dealing with loop, control statement, and data structure. 

Other Java Coding Problems for Practice

Thanks for reading this article so far. If you like this solution of transposing a matrix in Java and my explanation then please share with your friends and colleagues. If you have any questions or feedback then please drop a note. 


No comments :

Post a Comment