Pages

Tuesday, October 27, 2020

Java Program to Find Transpose of a Matrix

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.

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

Java Program to Find Transpose of a Matrix


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.

result[j][i] = matrix[i][j];
 


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:

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.

Ref

No comments:

Post a Comment

Please do not add any spam links in the comments section.