Transpose of a matrix in Java
In this post, you will learn different ways to transpose a matrix using the Java programming language.
The transpose of a matrix is simply a flipped version of the original matrix. We can transpose a matrix by switching its rows with its columns, i.e., write the elements of the rows as columns and write the elements of a column as rows. For example, the value in the 1st row and 3rd column ends up in the 3rd row and 1st column. In other words, transpose of B[][] is obtained by changing B[i][j] to B[j][i]. These are different ways to find the transpose of a matrix in Java -
Transpose of a matrix using Nested forloop
In the given program, we have used the nested forloop to iterate through each row and each column. In each iteration we place the matrix[j][i] element into transpose[i][j].
//Transpose of a matrix
// in Java
public class MatrixTranspose{
public static void main(String args[]){
//Creating a matrix
int matrix[][]={{23,42,12},{21,14,15},{29,31,13}};
//new matrix to store transpose of a matrix
int transpose[][]=new int[3][3];
//Code to find transpose a matrix
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
transpose[i][j]=matrix[j][i];
}
}
System.out.println("Original Matrix:");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
System.out.println("Transpose Matrix:");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(transpose[i][j]+" ");
}
System.out.println();
}
}
}
Output of the above code:
Original Matrix:
23 42 12
21 14 15
29 31 13
Transpose Matrix:
23 21 29
42 14 31
12 15 13
Transpose of a matrix using user input
In the given example, we have asked the user to enter the total rows and columns and the original matrix. Then, with the help of loops, we have obtained the transpose of a given matrix by interchanging all the rows and columns of a matrix.
// Java Program
// to find transpose of
// a matrix
import java.util.Scanner;
public class MatrixTranspose
{
public static void main(String args[])
{
int x, y;
System.out.println("Enter total rows and columns: ");
Scanner s = new Scanner(System.in);
int row = s.nextInt();
int column = s.nextInt();
int array[][] = new int[row][column];
System.out.println("Enter matrix:");
for(x = 0; x < row; x++)
{
for(y = 0; y < column; y++)
{
array[x][y] = s.nextInt();
System.out.print(" ");
}
}
System.out.println("Original Matrix : ");
for(x = 0; x < row; x++)
{
for(y = 0; y < column; y++)
{
System.out.print(array[x][y]+" ");
}
System.out.println(" ");
}
System.out.println("Matrix Transpose : ");
for(x = 0; x < column; x++)
{
for(y = 0; y < row; y++)
{
System.out.print(array[y][x]+" ");
}
System.out.println(" ");
}
}
}
Output of the above code:
Enter total rows and columns: 3 3
Enter matrix:
21 44 53
12 43 11
25 13 52
Original Matrix :
21 44 53
12 43 11
25 13 52
Matrix Transpose :
21 12 25
44 43 13
53 11 52
Transpose of a matrix using user defined method
In the given example, we have defined a user function to find the transpose of a matrix.
public class MatrixTranspose {
public static void main(String[] args) {
int row = 2, column = 3;
int[][] matrix = { {22, 12, 29}, {15, 51, 25} };
System.out.println("Original matrix : ");
// Printing original matrix
displayMatrix(matrix);
// Transpose the matrix
int[][] transpose = new int[column][row];
for(int x = 0; x < row; x++) {
for (int y = 0; y < column; y++) {
transpose[y][x] = matrix[x][y];
}
}
System.out.println("Matrix Transpose : ");
// Printing transposed matrix
displayMatrix(transpose);
}
public static void displayMatrix(int[][] matrix) {
for(int[] row : matrix) {
for (int column : row) {
System.out.print(column + " ");
}
System.out.println();
}
}
}
Output of the above code:
Original matrix :
22 12 29
15 51 25
Matrix Transpose :
22 15
12 51
29 25
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java