Find the Transpose of a Matrix in Java

In this article, you will learn how to find the Transpose of a Matrix using Java. To find the transpose of a given matrix in Java, you need to interchange the rows and columns of the original matrix.

Transpose Matrix:

The transpose of a matrix is a new matrix obtained by interchanging its rows and columns. In other words, if the original matrix has dimensions “m × n”, the transpose will have dimensions “n × m”. Each element at the ith row and jth column of the original matrix becomes the element at the jth row and ith column of the transpose matrix.

Here’s a Java program to find the Transpose of a Matrix:

package com.javacodepoint.array.multidimensional;

public class TransposeMatrix {
	public static int[][] transpose(int[][] matrix) {
		int rows = matrix.length;
		int cols = matrix[0].length;

		int[][] transposeMatrix = new int[cols][rows];

		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				transposeMatrix[j][i] = matrix[i][j];
		return transposeMatrix;
	public static void printMatrix(int[][] matrix) {
		for (int[] row : matrix) {
			for (int value : row) {
				System.out.print(value + " ");

	public static void main(String[] args) {
		int[][] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

		int[][] transposeMatrix = transpose(matrix);

		System.out.println("Original Matrix:");

		System.out.println("\nTranspose Matrix:");



Original Matrix:
1 2 3
4 5 6
7 8 9
Transpose Matrix:
1 4 7
2 5 8
3 6 9


In this program, we have a transpose method that takes a 2D integer array (matrix) as input and returns the transpose matrix as a new 2D array.

We first determine the number of rows and columns of the original matrix. Then, we create a new matrix called transposeMatrix, with the number of rows equal to the number of columns of the original matrix, and the number of columns equal to the number of rows of the original matrix.

We then use nested loops to iterate through the elements of the original matrix and assign them to the corresponding positions in the transpose matrix.

The printMatrix method is used to print the elements of the matrix.

Learn also: Symmetric Matrix Program.

Java logical programs list

Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *