
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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 Articles
- Java program to transpose a matrix.
- C++ Program to Find Transpose of a Matrix
- Java program to print the transpose of a matrix
- C++ Program to Find Transpose of a Graph Matrix
- Python Program to find the transpose of a matrix
- Golang Program To Find 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?
- Swift Program to Find the Transpose
- Golang Program To Find The Transpose

Advertisements