

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Find Transpose of a Matrix
In this article, we will understand how to find transpose of a matrix. The matrix has a row and column arrangement of its elements. A matrix with m rows and n columns can be called as m × n matrix. The transpose of a matrix is found by interchanging its rows into columns or columns into rows.
Below is a demonstration of the same −
Suppose our input is −
The matrix is defined as: 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4
The desired output would be −
The transpose of the matrix is: 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
Algorithm
Step 1 - START Step 2 - Declare two integer matrices namely input_matrix and result_matrix. Step 3 - Define the values. Step 4 - Iterate over each element of the matrix using two for-loops, assign the element at [i][j] position of the matrix to the [j][i] position of the result_matrix. Step 5 - Display the result_matrix. Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
public class MatrixTranspose { static final int matrix_size = 4; public static void main (String[] args) { int input_matrix[][] = { {1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3, 3}, {4, 4, 4, 4} }; System.out.println("The matrix is defined as: \n"); for (int i = 0; i < matrix_size; i++) { for (int j = 0; j < matrix_size; j++) { System.out.print(input_matrix[i][j] + " "); } System.out.println(); } int result_matrix[][] = new int[matrix_size][matrix_size]; for (int i = 0; i < matrix_size; i++) for (int j = 0; j < matrix_size; j++) result_matrix[i][j] = input_matrix[j][i]; System.out.println("\nThe transpose of the matrix is: "); for (int i = 0; i < matrix_size; i++) { for (int j = 0; j < matrix_size; j++) { System.out.print(result_matrix[i][j] + " "); } System.out.println(); } } }
Output
The matrix is defined as: 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 The transpose of the matrix is: 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
Example 2
Here, we encapsulate the operations into functions exhibiting object oriented programming.
public class MatrixTranspose { static final int matrix_size = 4; static void transpose(int input_matrix[][]) { int result_matrix[][] = new int[matrix_size][matrix_size]; for (int i = 0; i < matrix_size; i++) for (int j = 0; j < matrix_size; j++) result_matrix[i][j] = input_matrix[j][i]; System.out.println("\nThe transpose of the matrix is: "); for (int i = 0; i < matrix_size; i++) { for (int j = 0; j < matrix_size; j++) { System.out.print(result_matrix[i][j] + " "); } System.out.println(); } } public static void main (String[] args) { int input_matrix[][] = { {1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3, 3}, {4, 4, 4, 4} }; System.out.println("The matrix is defined as: \n"); for (int i = 0; i < matrix_size; i++) { for (int j = 0; j < matrix_size; j++) { System.out.print(input_matrix[i][j] + " "); } System.out.println(); } transpose(input_matrix); } }
Output
The matrix is defined as: 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 The transpose of the matrix is: 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
- Related Questions & Answers
- C++ Program to Find Transpose of a Matrix
- Java program to transpose a matrix.
- C++ Program to Find Transpose of a Graph Matrix
- Python Program to find the transpose of a matrix
- Java program to print the transpose of a matrix
- Find the transpose of a matrix in Python Program
- Transpose a matrix in Java
- Program to find the transpose of given matrix in Python
- How to calculate transpose of a matrix using C program?
- Transpose a matrix in C#
- Transpose a matrix in Python?
- How to Transpose a Matrix using Python?
- A C++ Program for 2d matrix for taking Transpose
- How to Transpose a matrix in Single line in Python?
- How to find the transpose of a tensor in PyTorch?
Advertisements