## \$show=/label

A quick programming guide on how to transpose a matrix in java with using for loops efficiently.

## 1. Overview

In this article, we'll learn how to find the transpose of a matrix in java using for loops.

Look at the below inputs and outputs for the matrix transpose.

Input:

1 2 3
4 5 6
7 8 9

Output:

1 4 7
2 5 8
3 6 9

This is just an interchange of the columns with rows or rows with columns.

## 2. Java Program To Find the Transpose for squared matrix

If the matrix rows and columns are the same then it is called a squared matrix.

Example 1:
```package com.javaprogramto.programs.arrays.matrix.transpose;

public class MatrixTransposeExample {

public static void main(String[] args) {

int[][] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

int rows = 3, columns = 3;
System.out.println("Original matrix");

// print matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}

// Logic to the transpose of matrix

int[][] transpose = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
transpose[i][j] = matrix[j][i];
}
}

// print transpose matrix
System.out.println("Transpose of matrix");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(transpose[i][j] + " ");
}
System.out.println();
}

}

}
```
Output:
```Original matrix
1 2 3
4 5 6
7 8 9
Transpose of matrix
1 4 7
2 5 8
3 6 9

```

## 3. Java Program To Find the Transpose for rectangle (non-squared) matrix

Next, find the transpose of the rectangle matrix in java.

If the matrix rows and columns lengths are not the same then it is a rectangular matrix.

The algorithm for this problem is simple.
we need to run the outer loop for a maximum of row and columns length and the inner loop runs for a low of two.

Example 2:

Look at the below example code.

```package com.javaprogramto.programs.arrays.matrix.transpose;

public class MatrixTransposeExample2 {

public static void main(String[] args) {

int[][] matrix = { { 10, 11, 12, 13 }, { 14, 15, 16, 17 }, { 18, 19, 20, 21 } };

int rows = 3, columns = 4;
System.out.println("Original matrix");

// print matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}

// Logic to the transpose of matrix

int[][] transpose = new int[columns][rows];
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
transpose[i][j] = matrix[j][i];
}
}

// print transpose matrix
System.out.println("Transpose of matrix");
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
System.out.print(transpose[i][j] + " ");
}
System.out.println();
}

}

}

```
Output:
```Original matrix
10 11 12 13
14 15 16 17
18 19 20 21
Transpose of matrix
10 14 18
11 15 19
12 16 20
13 17 21
```
If the columns and rows are handled properly, we get the runtime exception saying "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:"

## 4. Java - Find matrix transpose for any type of matrix

As of now, we have written the separate programs for the squared and rectangle matrix but now, let us simplify the algorithm into the single to handle both scenarios.

For this, we need to manipulate the columns and rows size correctly for the transpose matrix.

Example 3:
```package com.javaprogramto.programs.arrays.matrix.transpose;

public class MatrixTransposeExample3 {

public static void main(String[] args) {

int[][] matrix1 = { { 10, 11, 12, 13 }, { 14, 15, 16, 17 }, { 18, 19, 20, 21 } };

System.out.println("Rectangle matrix");
printTransposeMatrix(matrix1);

System.out.println("\nSquered matrix");
int[][] matrix2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
printTransposeMatrix(matrix2);
}

private static void printTransposeMatrix(int[][] matrix) {
int rows = matrix.length, columns = matrix.length;
System.out.println("Original matrix");

// print matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}

// Logic to the transpose of matrix

if (rows != columns) {
int temp = rows;
rows = columns;
columns = temp;
}

int[][] transpose = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
transpose[i][j] = matrix[j][i];
}
}

// print transpose matrix
System.out.println("Transpose of matrix");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(transpose[i][j] + " ");
}
System.out.println();
}

}

}
```

Output:
```Rectangle matrix
Original matrix
10 11 12 13
14 15 16 17
18 19 20 21
Transpose of matrix
10 14 18
11 15 19
12 16 20
13 17 21

Squered matrix
Original matrix
1 2 3
4 5 6
7 8 9
Transpose of matrix
1 4 7
2 5 8
3 6 9

```

## 5. Java - Inplace matrix transpose without additional matrix

Let us now discuss how an additional array can be eliminated for the inplace use.

But this logic works only for the squared matrix. because for the rectangular matrix, transpose size will be inverted so a new array for the transpose matrix has to be recreated.

Example 4:
```package com.javaprogramto.programs.arrays.matrix.transpose;

public class MatrixTransposeExample4 {

public static void main(String[] args) {

System.out.println("\nSquered matrix");
int[][] matrix2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
inplaceTransposeMatrix(matrix2);
}

private static void inplaceTransposeMatrix(int[][] matrix) {
int rows = matrix.length, columns = matrix.length;
System.out.println("Original matrix");

// print matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}

for (int i = 0; i < rows; i++) {
for (int j = i + 1; j < columns; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}

// print transpose matrix
System.out.println("Transpose of matrix");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}

}

}
```

Output:
```Squered matrix
Original matrix
1 2 3
4 5 6
7 8 9
Transpose of matrix
1 4 7
2 5 8
3 6 9

```

## 6. Conclusion

In this article, we've seen how to find the transpose of squared and rectangular matrices in java. BLOGGER

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java - How To Find Transpose Of A Matrix in Java in 4 ways?
Java - How To Find Transpose Of A Matrix in Java in 4 ways?
A quick programming guide on how to transpose a matrix in java with using for loops efficiently.