Perform Matrix Transpose in Java
What you’ll learn
- What the transpose AT means: turn rows into columns so
(AT)ij = Aji. - How to implement it with a second buffer
transposed[cols][rows]and nested loops. - A 2×3 example (output shape 3×2), a 3×3 example, plus a live preview.
Prerequisites
2D arrays, nested loops, and matrix index mapping in Java.
- Java basics:
class Main,mainmethod, and printing withSystem.out.print. - Indexing with
matrix[row][col], and knowing that transpose swaps tomatrix[col][row].
What is the transpose?
Transpose swaps row and column positions: (AT)ij = Aji. So an m x n matrix becomes n x m.
Math definition
For matrix A, transpose AT is defined by (AT)ij = Aji. An m x n matrix becomes n x m.
Live preview
Algorithm
Goal: given matrix[rows][cols], fill transposed[cols][rows] with transposed[i][j] = matrix[j][i].
📜 Pseudocode
Pseudocode
function transpose(matrix, rows, cols, out): // out is cols × rows
for i from 0 to cols - 1:
for j from 0 to rows - 1:
out[i][j] = matrix[j][i]1
Transpose a 2×3 matrix
java
public class Main {
static void transposeMatrix(int[][] matrix, int rows, int cols) {
int[][] transposed = new int[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
transposed[i][j] = matrix[j][i];
}
}
System.out.println("Original (" + rows + " x " + cols + "):");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
System.out.println("Transposed Matrix (" + cols + " x " + rows + "):");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
System.out.print(transposed[i][j] + "\t");
}
System.out.println();
}
}
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
int rows = 2;
int cols = 3;
transposeMatrix(matrix, rows, cols);
}
}2
Transpose a 3×3 matrix
java
public class Main {
static final int N = 3;
static void transposeSquare(int[][] a, int[][] out) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
out[i][j] = a[j][i];
}
}
}
static void printMatrix(String title, int[][] m) {
System.out.println(title);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(m[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int[][] a = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[][] t = new int[N][N];
transposeSquare(a, t);
printMatrix("A", a);
System.out.println();
printMatrix("A^T", t);
}
}Optimization
Square matrices can be transposed in place by swapping across diagonal; rectangular matrices usually need output buffer.
❓ FAQ
Take every row of the original matrix and write it as a column of a new matrix (or swap row index with column index at each entry).
It has 3 rows and 2 columns. In general an m×n matrix becomes n×m.
The entry at row i, column j of A^T equals the entry at row j, column i of A. In symbols: (A^T)_ij = A_ji.
In general a non-square matrix changes shape, so you usually need a second buffer. For a square n×n matrix you can swap across the diagonal in place with care.
So each assignment transposed[i][j] = matrix[j][i] does not overwrite data you still need from the original matrix.
You touch every element once to fill the transpose: O(m·n) time for an m×n matrix, with O(m·n) space for the output array.
🔄 Input / output examples
[[1,2,3],[4,5,6]] -> [[1,4],[2,5],[3,6]].
Edge cases
Empty or jagged arrays need validation; this page assumes proper rectangular matrices.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
Transpose m × n | O(m · n) | O(m · n) for output buffer |
Summary
- Transpose swaps row and column indices.
- Cost is
O(m * n).
Did you know?
The transpose flips a matrix across its diagonal: rows become columns. If A is m × n, then AT is n × m, and (AT)T = A.
8 people found this page helpful
