# Transpose of a Matrix in Java

Transpose of a Matrix in Java | Java Program to transpose 2D array | In this post, we will discuss what is the transpose of a matrix and how to write a Java program to find the transpose of a matrix?

What is the Transpose of a Matrix?

Let A =[aij] be an m × n matrix. The transpose of A, denoted by At, is the n × m matrix obtained by interchanging the rows and columns of A. In other words, if At =[bij], then bij = aji for i = 1,2,…,n and j = 1,2,…,m.

For 3×2 Matrix,

Original Matrix
`a11 a12a21 a22a31 a32`

Transpose Matrix
`a11 a21 a31a12 a22 a32`

Example using 2×2 matrix:-

``````     1   2
A =
3   4``````

Then the transpose of a matrix,

``````     1   3
At =
2   4``````

Java Method to find the transpose of a Matrix

``````// method to calculate the transpose of a matrix
public static int[][] transposeMatrix(int[][] a) {

// calculate row and column size
int row = a.length;
int column = a.length;

// declare a matrix to store resultant
int temp[][] = new int[row][column];

// calculate transpose of matrix
// outer loop for row
for (int i = 0; i < row; i++) {
// inner loop for column
for (int j = 0; j < column; j++) {
// formula
temp[i][j] = a[j][i];
}
}

// return resultant matrix
return temp;
}``````

Time Complexity:- O(N2)

Assuming the matrix is a square matrix then the size of the row and column will be similar. Then the above Java method uses two loops (from 1 to n) to find the transpose of the matrix therefore the time complexity for the method is O(N2).

## Java Program to Find Transpose of a Matrix

``````import java.util.Arrays;

public class Matrix {

// main method
public static void main(String[] args) {

// declare and initialize a matrix
int a[][] = { { 1, 2 }, { 8, 9 } };

// find row and column size
int row = a.length;
int column = a.length;

// declare new matrix to store result
int transpose[][] = new int[row][column];

// Transpose of matrix
transpose = transposeMatrix(a);

// display all matrices
System.out.println("A = " + Arrays.deepToString(a));
System.out.println("Transpose = " +
Arrays.deepToString(transpose));
}

// method to calculate the transpose of a matrix
public static int[][] transposeMatrix(int[][] a) {

// calculate row and column size
int row = a.length;
int column = a.length;

// declare a matrix to store resultant
int temp[][] = new int[row][column];

// calculate transpose of matrix
// outer loop for row
for (int i = 0; i < row; i++) {
// inner loop for column
for (int j = 0; j < column; j++) {
// formula
temp[i][j] = a[j][i];
}
}

// return resultant matrix
return temp;
}

}``````

Output:-

A = `[[`1, 2`]`, `[`8, 9`]]`
Transpose = `[[`1, 8`]`, `[`2, 9`]]`

In this program, to display the matrix we had used deepToString() method of the Arrays class, but you can also use the nested loops. See:- Different ways to print array in Java

## Program by taking Input from the User

In the above program both matrices A and B were initialized within the program, now let us see another program for transpose of a matrix by taking input value from the end-user using the Scanner class. If you want then you can also use BufferedReader class.

``````import java.util.Scanner;

public class Matrix {

// main method
public static void main(String[] args) {

// create Scanner class object to read input
Scanner scan = new Scanner(System.in);

// declare variables
int row = 0;
int column = 0;
int a[][] = null; // first matrix
int transpose[][] = null; // resultant matrix

// ask row and column size
System.out.println("Enter row and column size: ");
row = scan.nextInt();
column = scan.nextInt();

// initialize matrices
a = new int[row][column];
transpose = new int[row][column];

System.out.println("Enter Matrix A: ");
for(int i=0; i<row; i++) {
for(int j=0; j<column; j++) {
a[i][j] = scan.nextInt();
}
}

// transpose of matrix
transpose = transposeMatrix(a);

// display resultant matrix
System.out.println("Transpose =");
for(int i=0; i<transpose.length; i++) {
for(int j=0; j<transpose.length; j++) {
System.out.print(transpose[i][j]+" ");
}
System.out.println(); // new line
}

// close Scanner
scan.close();
}

// method to calculate the transpose of a matrix
public static int[][] transposeMatrix(int[][] a) {

// calculate row and column size
int row = a.length;
int column = a.length;

// declare a matrix to store resultant
int temp[][] = new int[row][column];

// calculate transpose of matrix
// outer loop for row
for (int i = 0; i < row; i++) {
// inner loop for column
for (int j = 0; j < column; j++) {
// formula
temp[i][j] = a[j][i];
}
}

// return resultant matrix
return temp;

}
}``````

Output:-

Enter row and column size:
3 3
Enter Matrix A:
1 2 3
4 5 6
7 8 9
Transpose =
1 4 7
2 5 8
3 6 9

Inside the main method, first, we had created the Scanner class object to read input value. Then we had initialized the required variables. After then row and column values have been read from the end-user. Later matrix is initialized with the default value and next to that matrix elements are filled in the matrix by taking input values from the end-user. After that transposeMatrix() method is called which returns the transpose of the passed matrix or 2D array. Finally, matrices are displayed on the screen.

See more matrix programs in Java:-

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!