Pages

Sunday, November 21, 2021

Java - How To Find Transpose Of A Matrix in Java in 4 ways?

1. Overview

In this article, we'll learn how to find the transpose of a matrix in java using for loops.

Look at the below inputs and outputs for the matrix transpose.

Input:

1 2 3
4 5 6
7 8 9

Output:

1 4 7
2 5 8
3 6 9

This is just an interchange of the columns with rows or rows with columns.

Java - How To Find Transpose Of A Matrix in Java in 4 ways?



2. Java Program To Find the Transpose for squared matrix


If the matrix rows and columns are the same then it is called a squared matrix.

Example 1:
package com.javaprogramto.programs.arrays.matrix.transpose;

public class MatrixTransposeExample {

	public static void main(String[] args) {

		int[][] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

		int rows = 3, columns = 3;
		System.out.println("Original matrix");

		// print matrix
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				System.out.print(matrix[i][j] + " ");
			}
			System.out.println();
		}

		// Logic to the transpose of matrix

		int[][] transpose = new int[rows][columns];
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				transpose[i][j] = matrix[j][i];
			}
		}
		
		// print transpose matrix
		System.out.println("Transpose of matrix");
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				System.out.print(transpose[i][j] + " ");
			}
			System.out.println();
		}

	}

}
Output:
Original matrix
1 2 3 
4 5 6 
7 8 9 
Transpose of matrix
1 4 7 
2 5 8 
3 6 9 


3. Java Program To Find the Transpose for rectangle (non-squared) matrix


Next, find the transpose of the rectangle matrix in java.

If the matrix rows and columns lengths are not the same then it is a rectangular matrix.

The algorithm for this problem is simple. 
we need to run the outer loop for a maximum of row and columns length and the inner loop runs for a low of two.

Example 2:

Look at the below example code.

package com.javaprogramto.programs.arrays.matrix.transpose;

public class MatrixTransposeExample2 {

	public static void main(String[] args) {

		int[][] matrix = { { 10, 11, 12, 13 }, { 14, 15, 16, 17 }, { 18, 19, 20, 21 } };

		int rows = 3, columns = 4;
		System.out.println("Original matrix");

		// print matrix
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				System.out.print(matrix[i][j] + " ");
			}
			System.out.println();
		}

		// Logic to the transpose of matrix

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

		// print transpose matrix
		System.out.println("Transpose of matrix");
		for (int i = 0; i < columns; i++) {
			for (int j = 0; j < rows; j++) {
				System.out.print(transpose[i][j] + " ");
			}
			System.out.println();
		}

	}

}

Output:
Original matrix
10 11 12 13 
14 15 16 17 
18 19 20 21 
Transpose of matrix
10 14 18 
11 15 19 
12 16 20 
13 17 21 
If the columns and rows are handled properly, we get the runtime exception saying "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:"

4. Java - Find matrix transpose for any type of matrix


As of now, we have written the separate programs for the squared and rectangle matrix but now, let us simplify the algorithm into the single to handle both scenarios.

For this, we need to manipulate the columns and rows size correctly for the transpose matrix.

Example 3:
package com.javaprogramto.programs.arrays.matrix.transpose;

public class MatrixTransposeExample3 {

	public static void main(String[] args) {

		int[][] matrix1 = { { 10, 11, 12, 13 }, { 14, 15, 16, 17 }, { 18, 19, 20, 21 } };

		System.out.println("Rectangle matrix");
		printTransposeMatrix(matrix1);

		System.out.println("\nSquered matrix");
		int[][] matrix2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
		printTransposeMatrix(matrix2);
	}

	private static void printTransposeMatrix(int[][] matrix) {
		int rows = matrix.length, columns = matrix[0].length;
		System.out.println("Original matrix");

		// print matrix
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				System.out.print(matrix[i][j] + " ");
			}
			System.out.println();
		}

		// Logic to the transpose of matrix

		if (rows != columns) {
			int temp = rows;
			rows = columns;
			columns = temp;
		}

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

		// print transpose matrix
		System.out.println("Transpose of matrix");
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				System.out.print(transpose[i][j] + " ");
			}
			System.out.println();
		}

	}

}

Output:
Rectangle matrix
Original matrix
10 11 12 13 
14 15 16 17 
18 19 20 21 
Transpose of matrix
10 14 18 
11 15 19 
12 16 20 
13 17 21 

Squered matrix
Original matrix
1 2 3 
4 5 6 
7 8 9 
Transpose of matrix
1 4 7 
2 5 8 
3 6 9 

5. Java - Inplace matrix transpose without additional matrix


Let us now discuss how an additional array can be eliminated for the inplace use.

But this logic works only for the squared matrix. because for the rectangular matrix, transpose size will be inverted so a new array for the transpose matrix has to be recreated.

Example 4:
package com.javaprogramto.programs.arrays.matrix.transpose;

public class MatrixTransposeExample4 {

	public static void main(String[] args) {

		System.out.println("\nSquered matrix");
		int[][] matrix2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
		inplaceTransposeMatrix(matrix2);
	}

	private static void inplaceTransposeMatrix(int[][] matrix) {
		int rows = matrix.length, columns = matrix[0].length;
		System.out.println("Original matrix");

		// print matrix
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				System.out.print(matrix[i][j] + " ");
			}
			System.out.println();
		}

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

		// print transpose matrix
		System.out.println("Transpose of matrix");
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				System.out.print(matrix[i][j] + " ");
			}
			System.out.println();
		}

	}

}


Output:
Squered matrix
Original matrix
1 2 3 
4 5 6 
7 8 9 
Transpose of matrix
1 4 7 
2 5 8 
3 6 9 

6. Conclusion


In this article, we've seen how to find the transpose of squared and rectangular matrices in java.



No comments:

Post a Comment

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