C Matrix Transpose Program

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

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].

Remember
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 shapeAᵀ shapeExample map
2 × 33 × 2A[0][2]=3 → Aᵀ[2][0]=3
3 × 33 × 3Square — size unchanged
1 × 44 × 1Row becomes a column

How to Transpose a Matrix in C

Four clear steps — new buffer for any shape, or in-place swaps for squares.

  1. Read rows m and columns n of A.
  2. Allocate result of size n × m (or reuse A if square).
  3. For every i, j, copy A[i][j] into result[j][i].
  4. For square in-place: swap A[i][j] with A[j][i] when i < j.
MethodIdeaTimeBest for
New bufferresult[j][i] = A[i][j]O(m × n)Any shape, interviews
In-place squareSwap above-diagonal pairsO(n²)Square matrices, less memory

Pseudocode

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

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Rule(Aᵀ)[i][j] = A[j][i]
Shapem × n → n × m
Twice(Aᵀ)ᵀ = A
In-placeSquare only (simple form)
IndexingWrite to [j][i], read from [i][j]
Classic check{{1,2,3},{4,5,6}}ᵀ = {{1,4},{2,5},{3,6}}

Live Preview

Pick a sample matrix to see A and its transpose Aᵀ.

Tap a chip to run. Rectangular samples change shape; square stays square.

Result
A (2×3) =
1  2  3
4  5  6

Aᵀ (3×2) =
1  4
2  5
3  6

Worked Walkthrough — 2×3

Copy each A[i][j] into T[j][i].

Calculation
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]

C 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].

C
#include <stdio.h>

#define ROWS 2
#define COLS 3

void transpose(int a[ROWS][COLS], int t[COLS][ROWS])
{
    int i, j;

    for (i = 0; i < ROWS; i++)
    {
        for (j = 0; j < COLS; j++)
        {
            t[j][i] = a[i][j];
        }
    }
}

void print_transpose(int t[COLS][ROWS])
{
    int i, j;

    for (i = 0; i < COLS; i++)
    {
        for (j = 0; j < ROWS; j++)
        {
            printf("%d ", t[i][j]);
        }
        printf("\\n");
    }
}

int main(void)
{
    int a[ROWS][COLS] = { { 1, 2, 3 }, { 4, 5, 6 } };
    int t[COLS][ROWS];

    transpose(a, t);
    print_transpose(t);
    return 0;
}

How It Works

  1. ROWS = 2 and COLS = 3 describe the input shape.
  2. Allocate t with shape COLS × ROWS (swapped dimensions).
  3. Outer loop walks original rows; inner loop walks original columns.
  4. Write each value to the swapped index: t[j][i] = a[i][j].
  5. 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.

C
#include <stdio.h>

#define N 3

void transpose_in_place(int a[N][N])
{
    int i, j, temp;

    for (i = 0; i < N; i++)
    {
        for (j = i + 1; j < N; j++)
        {
            temp = a[i][j];
            a[i][j] = a[j][i];
            a[j][i] = temp;
        }
    }
}

int main(void)
{
    int a[N][N] = {
        { 1, 2, 3 },
        { 4, 5, 6 },
        { 7, 8, 9 }
    };
    int i, j;

    transpose_in_place(a);

    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
        {
            printf("%d ", a[i][j]);
        }
        printf("\\n");
    }
    return 0;
}

How It Works

  1. This form only works for square N × N matrices.
  2. Loop i from 0 to N-1.
  3. For each i, only visit j > i (above the diagonal).
  4. Swap a[i][j] with a[j][i]; the diagonal stays put.
  5. Result: rows become columns inside the same array.

Edge Cases & Pitfalls

Check these before calling the solution done.

Rectangular

Size changes

m × n becomes n × m — allocate the new shape.

In-place

Square 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.

Diagonal

Stays put

In-place: never swap a cell with itself (i == j).

Time and Space Complexity

ProgramTimeExtra space
New bufferO(m × n)O(m × n) for result
In-place squareO(n²)O(1)

Every entry is read and written once. In-place only saves memory when the matrix is square.

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.

Next: Maximum of an Array

Find the largest value in a one-dimensional array.

Maximum of array tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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