Java Matrix Transpose Program
Definition
What Is Matrix Transpose?
The transpose of a matrix flips it across the diagonal: rows become columns. If A is m × n, then AT is n × m and (AT)[i][j] = A[j][i].
A (2 × 3) Aᵀ (3 × 2)
[ 1 2 3 ] [ 1 4 ]
[ 4 5 6 ] → [ 2 5 ]
[ 3 6 ]
Rule: (Aᵀ)[i][j] = A[j][i]
Also: (Aᵀ)ᵀ = A
| A shape | Aᵀ shape | Example map |
|---|
2 × 3 | 3 × 2 | A[0][2]=3 → Aᵀ[2][0]=3 |
3 × 3 | 3 × 3 | Square — size unchanged |
1 × 4 | 4 × 1 | Row becomes a column |
Approach
How to Transpose a Matrix in Java
Four clear steps — new buffer for any shape, or in-place swaps for squares.
- Read rows
m and columns n of A. - Allocate result of size
n × m (or reuse A if square). - For every
i, j, copy A[i][j] into result[j][i]. - For square in-place: swap
A[i][j] with A[j][i] when i < j.
| Method | Idea | Time | Best for |
|---|
| New buffer | result[j][i] = A[i][j] | O(m × n) | Any shape, interviews |
| In-place square | Swap above-diagonal pairs | O(n²) | Square matrices, less memory |
Pseudocode
function Transpose(A):
m = rows of A
n = cols of A
T = new matrix[n][m]
for i from 0 to m - 1:
for j from 0 to n - 1:
T[j][i] = A[i][j]
return T
Reference
Cheat Sheet
Keep these facts handy while writing the program.
| Topic | Detail |
|---|
| Rule | (Aᵀ)[i][j] = A[j][i] |
| Shape | m × n → n × m |
| Twice | (Aᵀ)ᵀ = A |
| In-place | Square only (simple form) |
| Indexing | Write to [j][i], read from [i][j] |
| Classic check | {{1,2,3},{4,5,6}}ᵀ = {{1,4},{2,5},{3,6}} |
Try it
Live Preview
Pick a sample matrix to see A and its transpose Aᵀ.
Trace
Worked Walkthrough — 2×3
Copy each A[i][j] into T[j][i].
A = [1 2 3]
[4 5 6]
T is 3 × 2 (zeros first)
A[0][0]=1 → T[0][0]=1
A[0][1]=2 → T[1][0]=2
A[0][2]=3 → T[2][0]=3
A[1][0]=4 → T[0][1]=4
A[1][1]=5 → T[1][1]=5
A[1][2]=6 → T[2][1]=6
T = [1 4]
[2 5]
[3 6]
Code
Java Programs
Start with a new-buffer transpose that works for any shape. Use in-place swaps when the matrix is square and memory matters.
Example 1 — New Buffer
Create an n × m result and set result[j][i] = A[i][j].
public class MatrixTranspose {
static int[][] transpose(int[][] a) {
int m = a.length;
int n = a[0].length;
int[][] t = new int[n][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
t[j][i] = a[i][j];
}
}
return t;
}
static void print(int[][] m) {
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[i].length; j++) {
System.out.print(m[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int[][] a = { { 1, 2, 3 }, { 4, 5, 6 } };
print(transpose(a));
}
}
How It Works
m = rows of A, n = columns of A.- Allocate
t with shape n × m (swapped dimensions). - Outer loop walks original rows; inner loop walks original columns.
- Write each value to the swapped index:
t[j][i] = a[i][j]. - A
2×3 input becomes a 3×2 output.
Example 2 — In-Place Square
For an n × n matrix, swap above-diagonal pairs — no second matrix needed.
public class MatrixTransposeInPlace {
static void transposeInPlace(int[][] a) {
int n = a.length;
if (a[0].length != n) {
throw new IllegalArgumentException("In-place transpose needs a square matrix.");
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}
}
public static void main(String[] args) {
int[][] a = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
transposeInPlace(a);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
How It Works
- Reject non-square inputs — in-place shape cannot change.
- Loop
i from 0 to n-1. - For each
i, only visit j > i (above the diagonal). - Swap
a[i][j] with a[j][i]; the diagonal stays put. - Result: rows become columns inside the same array.
Edge Cases & Pitfalls
Check these before calling the solution done.
RectangularSize changes
m × n becomes n × m — allocate the new shape.
In-placeSquare only
Simple swaps cannot resize a rectangular matrix.
Index swap[j][i] not [i][j]
Writing to the wrong order leaves the matrix unchanged or wrong.
DiagonalStays put
In-place: never swap a cell with itself (i == j).
Analysis
Time and Space Complexity
| Program | Time | Extra space |
|---|
| New buffer | O(m × n) | O(m × n) for result |
| In-place square | O(n²) | O(1) |
Every entry is read and written once. In-place only saves memory when the matrix is square.
Remember
Key Takeaways
Rule: (Aᵀ)[i][j] = A[j][i].
Shape: m × n becomes n × m.
In-place: square only — swap i < j pairs.
Test: {{1,2,3},{4,5,6}}ᵀ → {{1,4},{2,5},{3,6}}.
One line: flip rows into columns with T[j][i] = A[i][j].
Frequently Asked Questions
The transpose Aᵀ flips A across its diagonal: rows become columns. If A is m × n, then Aᵀ is n × m, and (Aᵀ)[i][j] = A[j][i].
No. An n × n matrix stays n × n. A rectangular m × n matrix becomes n × m.
Yes for square matrices by swapping A[i][j] with A[j][i] for i < j. Rectangular matrices need a new array (or a different storage scheme).
Transposing twice returns the original matrix: (Aᵀ)ᵀ = A.
You visit every entry once, so time is O(m × n). Extra space is O(m × n) for a new buffer, or O(1) for in-place square transpose.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful