Java program to find the transpose of a matrix (of any order), we interchange its rows and columns to obtain the transpose.
Matrix transpose in Java
class TransposeAMatrix
{
public static void main(String args[])
{
int m, n, c, d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix");
m = in.nextInt();
n = in.nextInt();
int matrix[][] = new int[m][n];
System.out.println("Enter elements of the matrix");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
matrix[c][d] = in.nextInt();
int transpose[][] = new int[n][m];
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
transpose[d][c] = matrix[c][d];
System.out.println("Transpose of the matrix:");
for (c = 0; c < n; c++)
{
for (d = 0; d < m; d++)
System.out.print(transpose[c][d]+"\t");
System.out.print("\n");
}
}
}
Download Transpose matrix program class file.
Output of program:
The program can be used to check if a matrix is symmetric or not. We compare a matrix with its transpose, if both are the same then it's symmetric otherwise non-symmetric. It's also useful for calculating the orthogonality of a matrix.