Matrix transpose in Java
In this post, you will learn different ways to perform matrix transpose using the Java programming language.
Matrix, a set of numbers arranged in rows and columns so as to form a rectangular array. Matrices are utilized substantially more in everyday life than individuals would have suspected. A square matrix can represent a linear transformation of a geometric object. A real-life example is Adobe Photoshop, it uses a matrix to process linear transformations to render images. In robotics and automation, matrices are the fundamental building blocks for robot development. The contributions for controlling robots are acquired based on the calculations from matrices.
The transpose of a matrix is simply a flipped version of the original matrix. We can transpose a matrix by switching its rows with its columns, i.e., write the elements of the rows as columns and write the elements of a column as rows. For example, the value in the 1st row and 3rd column ends up in the 3rd row and 1st column. In other words, transpose of B[][] is obtained by changing B[i][j] to B[j][i]. These are different ways to find the transpose of a matrix in Java programming language.
The different operations on the matrix are also very important for us. Let us discuss how to perform matrix transpose-
Transpose of a Matrix in Java using for loop
In the given Java program, we have used nested for loops to iterate through each row and each column. In each iteration, we place the matrix[i][j] element into transpose[j][i].
public class MatrixTransposeProgram{
public static void main(String args[]){
//creating a matrix
int matrix[][]={{6,5,2},{4,6,3},{9,8,2}};
//creating matrix to store transpose of a matrix
int transpose[][]=new int[3][3];
// Displaying original matrix
System.out.println("Original Matrix: ");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
//Code to transpose a matrix
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
transpose[i][j]=matrix[j][i];
}
}
// Displaying transpose of a matrix
System.out.println("Transpose of the matrix: ");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(transpose[i][j]+" ");
}
System.out.println();
}
}
}
Output of the above code:
Original Matrix:
6 5 2
4 6 3
9 8 2
Transpose of the matrix:
6 4 9
5 6 8
2 3 2
Transpose of a Matrix in Java using user inputs
In the given Java program, we ask the user to provide the number of rows, columns, and elements of the matrix. Next, using the loop iteration, we get the transpose of the matrix.
import java.util.Scanner;
public class MatrixTransposeProgram
{
public static void main(String args[])
{
int i, j;
System.out.println("Enter rows and columns: ");
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
int column = sc.nextInt();
int array[][] = new int[row][column];
System.out.println("Enter elements of the matrix:");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
array[i][j] = sc.nextInt();
System.out.print(" ");
}
}
System.out.println("Transpose of the matrix:");
for(i = 0; i < column; i++)
{
for(j = 0; j < row; j++)
{
System.out.print(array[j][i]+" ");
}
System.out.println(" ");
}
}
}
Output of the above code:
Enter rows and columns: 3 3
Enter elements of the matrix:
8 9 2
7 9 4
8 4 2
Transpose of the matrix:
8 7 8
9 9 4
2 4 2
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java