Home » Java Programs » Java Program to Transpose a Matrix

# Java Program to Transpose a Matrix

Java Program to Transpose a Matrix. A matrix is a rectangular array of numbers that are arranged in rows and columns. The numbers are referred to as the matrix’s elements or entries. Engineering, physics, economics, biology, and statistics, as well as various disciplines of mathematics, all use matrices.

## Uses of Matrices

Matrices can be used to write and work with many linear equations (a system of linear equations) in a compact manner. When it comes to linear transformations, often known as linear maps, matrices and matrix multiplication show their true colors.

They are used to create graphs, calculate statistics, and conduct scientific studies and research in a variety of subjects. Matrices can also be used to represent real-world information such as population, infant mortality rate, and so on. For charting surveys, these are the finest representation methods.

## How to Implement Transpose Matrix

The number of rows and columns in a matrix expressed as row columns. The matrix below comprises 2 rows and 3 columns, for a total of 23 dimensions. “Two by three,” is read aloud. The number of columns in the first matrix must equal the number of rows in the second matrix for matrix multiplication to be defined.

We use the dot product of a row in A and a column in B to find A B AB AB. Simply write the components of a row as columns and the elements of a column as rows to calculate the transpose of a matrix.

## Difference between 2* 2 matrix and 3* 3 matrix

The 2×2 Matrix is a decision-making technique in which possibilities are plotted on a two-by-two matrix. A four-blocker is also known as a magic quadrant. A simple square divided into four equal quadrants forms the matrix diagram. Each axis indicates a criterion for making a decision, such as a price or effort.

Determinants are special numbers computed from the square matrix in matrices. For a matrix with three rows and three columns, the determinant of a 3 x 3 matrix is determined. The determinant is represented by vertical lines on either side of the sign, such as | |.

## Java Program to Transpose a Matrix

``````import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i,j,row,col,temp;
System.out.println("Enter the number of rows");
row = sc.nextInt();
System.out.println("Enter the number of columns");
col = sc.nextInt();
int[][] mat = new int[row][col];
System.out.println("Enter the elements of the matrix") ;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
mat[i][j] = sc.nextInt();
}
}
System.out.println("The elements of the matrix") ;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
System.out.print(mat[i][j]+"\t");
}
System.out.println("");
}
for(i=0;i<row;i++)
{
for(j=0;j<i;j++)
{
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
System.out.println("The transpose of the matrix is ") ;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
System.out.print(mat[i][j]+"\t");
}
System.out.println("");
}
}
}``````

Output:

Enter the number of rows
2
Enter the number of columns
2
Enter the elements of the matrix
45
67
78
34
The elements of the matrix
45 67
78 34
The transpose of the matrix is
45 78
67 34