Algorithms · Visualized

Matrix: Transpose

Matrix & 2D

Rotations, traversals, and grid searches on 2D arrays.

Transposing a matrix flips it over its main diagonal: the element at row i, column j moves to row j, column i. Time: O(m×n) average, O(m×n) — every cell is read once worst; space: O(1) for a square matrix in place; O(m×n) when a rectangular result must be allocated.

Big-O: Classic O(m·n) sweeps and in-place tricks.

Transpose a Matrix

Matrix / 2D Array

Transposing a matrix flips it over its main diagonal: the element at row i, column j moves to row j, column i. Rows become columns and columns become rows, so an m×n matrix becomes n×m. For a square matrix the swap can be done in place by exchanging only the cells above the diagonal with their mirror cells below it.

How Transpose Works

  1. Read the dimensions: an m×n input produces an n×m output, so the result cannot alias the original unless the matrix is square.
  2. For a square matrix, loop i from 0 to n-1 and j from i+1 to n-1, swapping a[i][j] with a[j][i]. Starting j at i+1 skips the diagonal (which stays put) and avoids swapping each pair twice.
  3. For a rectangular matrix, allocate an n×m result and copy a[i][j] into result[j][i] for every cell.
  4. After the pass, position (i, j) of the result holds the input's (j, i) value; the diagonal itself is unchanged.

Transpose Worked Example

Input: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Swap (0,1)=2 with (1,0)=4 → [[1,4,3],[2,5,6],[7,8,9]]. Swap (0,2)=3 with (2,0)=7. Swap (1,2)=6 with (2,1)=8. Final transpose: [[1,4,7],[2,5,8],[3,6,9]].

Matrix: TransposeO(m·n)
Flip a matrix over its main diagonal.
1
2
3
4
5
6
7
8
9
Step 1 / 11
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Transpose 3×3 matrix → 3×3: cell (i,j) moves to (j,i).
Python · Matrix Algorithms in Code
def transpose_matrix(m):
    return [list(row) for row in zip(*m)]

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Transpose Time & Space Complexity

BestO(m×n)
AverageO(m×n)
WorstO(m×n) — every cell is read once
SpaceO(1) for a square matrix in place; O(m×n) when a rectangular result must be allocated

When to Use Transpose

  • Preparing image or sensor grids for algorithms that expect channels or features along the other axis.
  • Building block for in-place rotation and for symmetry checks such as a matrix being equal to its transpose.
  • Converting between row-major and column-major layouts when exchanging data with other libraries.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.