Java Program to Find Transpose of a Matrix
A quick and practical guide to calculate the matrix transpose in java. Transpose of a given matrix is nothing but the changing the values and order.
1. Overview
In this article, you’ll learn how to find the transpose of a given matrix using a simple for loop.
You can go thorough the previous articles on addition and multiplication of two matrices using arrays.
Transpose is nothing but a swapping the rows with columns and also order will be swapped. Finally, it produces the new matrix.
01 02 03 04 05 06 07 08 09 10 | Matrix M : [A11, A12 A21, A22 A31, A32] Transpose of Matrix M: [ A11, A21, A31 A12, A22, A32] |
Order of Transpose Matrix:
Matrix M order: 3 X 2
Transpose of Matrix M order: 2 X 3
2. Example Program to Find the Transpose Of Matrix
Need only one matrix to find the transpose. This can be done with only two for loops.
In the below program, added two methods doMatricTranspose() for generating the transpose of a matrix and other doPrintResultMatric() is to print the given matrix.
Moved the values from rows to columns. Input matrix order is 2 X 3 with 2 rows and 3 columns.
After calling the doMatricTranspose() and generated output matrix order is 3 X 2 with 3 rows and 2 columns. And also all the values of input matrix are swapped with its positions such as A12 to A21, A31 to A13 etc.
Core logic :
Main logic is added inside a separate function for reuse.
1 | result[j][i] = matrix[i][j]; |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | package com.javaprogramto.programs.arrays.matrix; public class MatrixTranspose { public static void main(String[] args) { // creating the first matrix using arrays int [][] matrix = { { 1 , 2 , 3 }, { 4 , 5 , 6 } }; //Printing the original matrix System.out.println( "Input Matrix : " ); doPrintResultMatric(matrix); // Matrix 1 rows and columns length int rows = matrix.length; int columns = matrix[ 0 ].length; // Calling a function for matrix transpose core logic int [][] result = doMatricTranspose(matrix, rows, columns); // printing the result System.out.println( "Transpose of Matrix : " ); doPrintResultMatric(result); } /** * Calculates the matrix transpose for given inputs arrays. * * @param matrix1 * @param rows1 * @param columns1 * @return */ private static int [][] doMatricTranspose( int [][] matrix, int rows, int columns) { // output array for storing the transpose result. order needs to be swapped. int [][] result = new int [columns][rows]; for ( int i = 0 ; i < rows; i++) { for ( int j = 0 ; j < columns; j++) { result[j][i] = matrix[i][j]; } } return result; } // prints the given matrix private static void doPrintResultMatric( int [][] result) { for ( int i = 0 ; i < result.length; i++) { for ( int j = 0 ; j < result[ 1 ].length; j++) { System.out.print(result[i][j] + " " ); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 | Input Matrix : 1 2 3 4 5 6 Transpose of Matrix : 1 4 2 5 3 6 |
3. Conclusion
In this article, You’ve seen how to get the transpose for a given matrix. The above example works for all types inputs with any order.
As usual, Shown example is over GitHub.
Published on Java Code Geeks with permission by Venkatesh Nukala, partner at our JCG program. See the original article here: Java Program to Find Transpose of a Matrix Opinions expressed by Java Code Geeks contributors are their own. |