Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The transpose of a matrix is found by interchanging the elements of the rows and columns. It is denoted using the letter “T” in the superscript of the given matrix and if initial dimensions of a matrix were (mxn) then, after the transpose the new dimensions of the transpose matrix become (nxm).
Problem Statement
Given: A matrix of mxn dimensions.
Problem: Find the transpose of a matrix and display the transpose matrix.
Sample Example
Let us look at the example of the following matrix to learn how to find the transpose of a matrix.
This matrix has the dimensions of 4x3, where we have 4 rows and 3 columns. The transpose of the matrix is defined by the matrix where the elements in the rows and columns are interchanged. Therefore, the transpose of the above matrix will be given as follows:
After the interchange of row and column elements, the new dimensions of the matrix are 3x4.
Brute Force Approach
In this approach, we will use two matrices, one is the given matrix and after iterating and interchanging values in the rows and columns we will store the element values in the second matrix.
Pseudocode
The pseudocode to find the transpose of a matrix is as follows:
Declare variables for rows and columns of the given matrix.
Initialize the matrix with the elements and print the original matrix.
Declare the second matrix for storing the transposed elements.
Store the elements in the transpose matrix by interchanging the elements at the ith and jth indices of the rows and columns.
Print the second matrix, i.e., the transpose matrix.
Implementation in Java
Java
Java
/*Program to find the transpose of a matrix*/ import java.util.*;
public class Main { public static void main(String []args) { Scanner s = new Scanner(System.in); int m,n; System.out.println("Enter the number of rows: \n"); m=s.nextInt();
System.out.println("Enter the number of column: \n"); n=s.nextInt();
int a1[][]=new int[m][n]; System.out.println("Enter the elements of the matrix: "); for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { a1[i][j]=s.nextInt(); } }
//Print the given matrix System.out.println("The given matrix is: "); for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { System.out.print(a1[i][j]+" "); } System.out.println(""); }
int a2[][]=new int[n][m]; // Find the transpose of the matrix for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { a2[j][i]=a1[i][j]; } }
Enter the number of rows: 4
Enter the number of column: 3
Enter the elements of the matrix: 2 9 3 13 11 17 3 6 15 4 13 1
The given matrix is:
2 9 3
13 11 17
3 6 15
4 13 1
The transpose matrix is:
2 13 3 4
9 11 6 13
3 17 15 1
Time Complexity: The time complexity of the program is O(mxn), where m and n are the dimensions of the matrix as we traverse through the complete matrix mxn times.
Space Complexity: The space complexity is O(mxn) because we need mxn space to store the (mxn) elements of the matrix.
Optimized Approach
In this approach, instead of using another matrix to store the transpose of a matrix, we will interchange the elements of the rows and columns in-place which will reduce the space complexity for the program. This approach can only be used in case of square matrices with nxn dimension because the number of rows of a square matrix is equal to the number of columns, and because of this the size of the original and the transpose matrix is the same.
Implementation in Java
Java
Java
import java.util.*;
public class Main { public static void main (String[] args) { int M[][] = {{2,9,3}, {13,11,17}, {3,6,15}, {4,13,1}}; int m = 4; int n = 3;
for(int i=0;i<m;i++){ for(int j=i;j<n;j++){
int temp = M[i][j]; M[i][j] = M[j][i]; M[j][i] = temp;
Time Complexity: The time complexity remains the same, i.e., O(mxn) where n is the number of rows and columns of the matrix and we traverse over (mxn) elements.
Space Complexity: The space complexity is reduced to O(1), because we perform operations in-place.
To transpose a matrix in Java, you can use nested loops. By iterating through each element of the matrix and swapping rows with columns, you can create a new matrix where the rows of the original matrix become columns and vice versa.
What is the fastest way to transpose a matrix?
The fastest way to transpose a matrix is to directly swap elements in place for square matrices, minimizing memory usage. For non-square matrices, using efficient algorithms or parallel processing can speed up the process, especially when working with large datasets.
What are in-place operations?
These are the operations which can be performed without using temporary variables for storing temporary values and the changes in the variables are directly made.
Which properties are followed by a transpose matrix?
The properties of a transpose matrix are multiplication by a constant, addition property and multiplication property of transpose.
What is the result when we take the transpose of a transpose matrix?
The result is the matrix itself when we take the transpose of a transpose matrix, because (A’)’ = A.
Conclusion
In this blog, we will learned the approach to finding the transpose of a matrix. The transpose is obtained by swapping the rows and columns of a matrix. It is denoted by adding a superscript “T” to the matrix. If the original matrix has dimensions (m x n), its transpose will have dimensions (n x m).
Refer to our guided paths on Code360 to upskill yourself! If you want to test your competency in coding, you may check out the mock test series and participate in the contests.