Java Program to Find Transpose of a Matrix

Transpose of a Matrix in Java is the new matrix formed by interchanging its rows and columns. The transpose of the matrix is denoted by using the letter “T” in the superscript of the given matrix.

For example, if “A” is the given matrix, then the transpose of the matrix is represented by A’ or AT.

Example 1:
Suppose we have a 3×3 matrix A:

\( A =
\begin{bmatrix}
1 & 2 & 3\\
4 & 5 & 6\\
7 & 8 & 9
\end{bmatrix}\)

To find the transpose of A, we simply interchange its rows and columns to obtain a new 3×3 matrix.

AT = \( \begin{bmatrix}
1 & 4 & 7\\
2 & 5 & 8\\
3 & 6 & 9
\end{bmatrix}\)

Here, the first row of A becomes the first column of AT, the second row of A becomes the second column of AT, and so on.

advertisement
advertisement

Example 2:
If the order of the original matrix is 2×3, the order of the transposed matrix will be 3×2, as shown below.

If the given matrix \( A =
\begin{bmatrix}
1 & 2 & 3\\
4 & 5 & 6
\end{bmatrix}\), then the transpose matrix (AT) is \( \begin{bmatrix}
1 & 4 \\
2 & 5\\
3 & 6
\end{bmatrix}\)

Problem Description

Write a Java Program to find the transpose of a given matrix.

Problem Solution

The transpose of a given matrix is calculated by interchanging the rows and columns of a matrix with the help of loops.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Program/Source Code

Here is the source code of the Java Program to Display Transpose Matrix. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Transpose
  3. {
  4.     public static void main(String args[])  
  5.     {
  6. 	int i, j;
  7. 	System.out.println("Enter total rows and columns: ");
  8. 	Scanner s = new Scanner(System.in);
  9. 	int row = s.nextInt();
  10. 	int column = s.nextInt();
  11. 	int array[][] = new int[row][column];
  12. 	System.out.println("Enter matrix:");
  13.  	for(i = 0; i < row; i++)
  14.   	{
  15.    	    for(j = 0; j < column; j++) 
  16.      	    {
  17.         	array[i][j] = s.nextInt();
  18.         	System.out.print(" ");
  19.             }
  20.   	}
  21. 	System.out.println("The above matrix before Transpose is ");
  22.   	for(i = 0; i < row; i++)
  23.     	{
  24.       	    for(j = 0; j < column; j++)
  25.             {
  26.           	System.out.print(array[i][j]+" ");
  27.             }
  28.             System.out.println(" ");
  29.         }
  30.  	System.out.println("The above matrix after Transpose is ");
  31.   	for(i = 0; i < column; i++)
  32.     	{
  33.       	    for(j = 0; j < row; j++)
  34.             {
  35.                 System.out.print(array[j][i]+" ");
  36.             }
  37.             System.out.println(" ");
  38.         }
  39.     }
  40. }
Program Explanation

1. Import Scanner class from java.util package.
2. Define a public class called Transpose.
3. Define a main method inside the class with String array as an argument.
4. Declare and initialize variables i, j to 0.
5. Print a message to the console to enter the total rows and columns of the matrix.
6. Create an object of the Scanner class and read the user input of rows and columns.
7. Create a 2D array of integer type using the input dimensions.
8. Print a message to the console to enter the elements of the matrix.
9. Use nested for loops to read user input and store them in the matrix.
10. Print the matrix before transpose.
11. Use nested for loops to calculate the transpose of the matrix and print it to the console.

Time Complexity: O(m*n)
In the program, we are iterating over the complete matrix, so time complexity is O(m*n), where m, n are the rows and columns of the matrix.

advertisement

Space Complexity: O(m*n)
Space is required to store the transpose of matrix, which is O(m*n), where m, n are the rows and columns of the matrix.

Runtime Test Cases

In this case, we enter the order of the matrix as “3 * 3” and the matrix elements as input to find the transpose of a matrix.

$ javac Transpose.java
$ java Transpose
 
Enter total rows and columns: 
3 3
Enter matrix:
1
2
3
4
5
6
7
8
9
The above matrix before Transpose is 
1 2 3  
4 5 6  
7 8 9  
The above matrix after Transpose is 
1 4 7  
2 5 8  
3 6 9

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.