Definition
(AT)ij = Aji
Swap row and column indices at every entry.
The transpose AT flips a matrix so rows become columns: (AT)ij = Aji, and an m × n matrix becomes n × m. This tutorial covers the index swap, a second buffer, a live preview, worked C examples (2×3 and 3×3), edge cases, and O(m·n) complexity.
(AT)ij = Aji
Swap row and column indices at every entry.
m×n → n×m
Height and width swap; 2×3 becomes 3×2.
out[cols][rows]
Copy into a new array so you do not overwrite source data.
out[i][j]=a[j][i]
One assignment expresses the whole idea.
2×3 → 3×2
See A and AT instantly in the browser.
(AT)T = A
Transpose twice to recover the original.
The transpose of a matrix A is written AT. Its entries satisfy (AT)ij = Aji: row and column indices are swapped.
So if A has m rows and n columns, AT has n rows and m columns. In C interviews you allocate a second buffer and copy with transposed[i][j] = matrix[j][i].
Transpose is a gentle but precise 2D-array drill: swapped dimensions, careful loop bounds, and a clear reason to use a second buffer before discussing in-place square tricks.
First row becomes first column.
Output shape flips with the indices.
Safest for rectangles and learners.
Touch each entry once to fill AT.
In short: allocate out[cols][rows], then set out[i][j] = matrix[j][i] for all valid i, j.
Given a matrix of size rows × cols, build its transpose of size cols × rows and print both.
/* A is 2×3 → A^T is 3×2
* A = 1 2 3 A^T = 1 4
* 4 5 6 2 5
* 3 6
* out[i][j] = a[j][i]
*/ | Item | Type | Description |
|---|---|---|
matrix | 2D array | Original; logical size rows × cols. |
transposed / out | 2D array | Result; size cols × rows. |
rows, cols | ints | Must match the filled slice of the array. |
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] | Approach | When | Notes |
|---|---|---|
| Second buffer (this page) | Any shape | Safest; required for non-square in fixed arrays |
| In-place swaps | Square only | Swap a[i][j] with a[j][i] for i < j |
| Addition / subtraction | Same shape ops | No index swap; different problem |
| Goal | Pattern |
|---|---|
| One cell | transposed[i][j] = matrix[j][i]; |
| Output shape | cols × rows when input is rows × cols |
| Loop bounds | i < cols, j < rows |
| Double transpose | (A^T)^T = A |
| Cost | O(m · n) time; O(m · n) for output buffer |
Transpose rearranges indices — it does not add, subtract, or multiply entries.
Aji → outThis page — swap indices
Aij+BijSame shape; no flip
Σ Aik BkjDot products; triple nest
copy firstSecond buffer before in-place
Reach for transpose when rows need to become columns, or when preparing factors for related linear-algebra steps.
Tests 2D indexing and swapped loop bounds.
Convert row-major tables to column-oriented views.
Building ATA or related forms conceptually.
Show why non-square needs a new shape buffer.
Changing shape usually requires separate storage.
Key benefit: one clear index-swap rule that proves you understand both shape changes and safe buffering.
Uses the same 2×3 sample as Example 1. Press the button to see A and AT.
Two complete C programs — a rectangle (2×3 → 3×2) and a square 3×3. Click View Output to reveal sample console results.
Allocate the flipped shape, then copy with swapped indices.
Classic layout: store the original in a fixed upper-bound array, pass real rows and cols, build transposed[cols][rows].
#include <stdio.h>
#define MAX 10
void transpose_matrix(int matrix[MAX][MAX], int rows, int cols) {
int transposed[MAX][MAX];
int i, j;
for (i = 0; i < cols; ++i) {
for (j = 0; j < rows; ++j) {
transposed[i][j] = matrix[j][i];
}
}
printf("Original (%d x %d):\n", rows, cols);
for (i = 0; i < rows; ++i) {
for (j = 0; j < cols; ++j) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
printf("Transposed Matrix (%d x %d):\n", cols, rows);
for (i = 0; i < cols; ++i) {
for (j = 0; j < rows; ++j) {
printf("%d\t", transposed[i][j]);
}
printf("\n");
}
}
int main(void) {
int matrix[MAX][MAX] = {
{1, 2, 3},
{4, 5, 6}
};
int rows = 2;
int cols = 3;
transpose_matrix(matrix, rows, cols);
return 0;
} The assignment transposed[i][j] = matrix[j][i] is the whole idea. Loop ranges follow the swapped dimensions: i runs cols times, j runs rows times.
Same index swap on a square matrix — output shape matches the input.
First row 1 2 3 becomes the first column of the result. Still uses a separate buffer (simplest and safest for learners).
#include <stdio.h>
#define N 3
void transpose_square(int a[N][N], int out[N][N]) {
int i, j;
for (i = 0; i < N; ++i) {
for (j = 0; j < N; ++j) {
out[i][j] = a[j][i];
}
}
}
void print_matrix(const char *title, int m[N][N]) {
int i, j;
printf("%s\n", title);
for (i = 0; i < N; ++i) {
for (j = 0; j < N; ++j) {
printf("%d ", m[i][j]);
}
printf("\n");
}
}
int main(void) {
int a[N][N] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int t[N][N];
transpose_square(a, t);
print_matrix("A", a);
printf("\n");
print_matrix("A^T", t);
return 0;
} For square matrices, out is the same shape as a. In-place transpose is possible for squares but easier to get wrong; two buffers keep the lesson clear.
Result has cols rows and rows columns when the input is rows × cols.
For each i in 0 .. cols-1 and j in 0 .. rows-1, assign out[i][j] = matrix[j][i].
Outer loop over rows of the transpose (cols), inner over columns (rows).
For the 2×3 sample, first column of AT is 1 2 3 from the first row of A.
Trace how the first row of the 2×3 sample becomes the first column of the transpose.
| Source | Value | Destination |
|---|---|---|
A[0][0] | 1 | AT[0][0] |
A[0][1] | 2 | AT[1][0] |
A[0][2] | 3 | AT[2][0] |
So the first column of AT is 1, 2, 3. The second source row fills the second column the same way.
Where transpose thinking shows up beyond the interview prompt.
Master [j][i] vs [i][j] carefully.
Example: double nested copy.
Prove you notice m×n becoming n×m.
Example: 2×3 sample.
Motivate a second array before in-place talk.
Example: rectangle case.
Transpose twice and compare to A.
Example: (AT)T = A.
Discuss optional in-place diagonal swaps.
Example: after the 3×3 demo.
Show transpose is rearrange, not arithmetic.
Example: after addition/multiply pages.
Pro Tip: say “m×n becomes n×m, then out[i][j] = a[j][i]” before writing any loops.
Why the second-buffer approach earns interview points.
Rectangles and squares use the same copy rule.
A separate buffer avoids overwriting values you still need.
O(m·n) time with an obvious output array.
First row of A should match first column of AT.
Pro Tip: mention in-place square transpose only as a follow-up after the correct buffered solution.
Small habits that keep transpose code clean in interviews.
Say m×n → n×m before coding.
Outer i < cols, inner j < rows for the buffered form.
They must match the filled portion of the array.
Confirm row 0 of A becomes column 0 of AT.
Ensure MAX ≥ max(rows, cols) for both dimensions when using one square buffer.
Pro Tip: the walkthrough table for the first column of AT is the fastest way to lock in the index swap before typing.
Mistakes that commonly break transpose solutions in C.
Looping i < rows when writing into a cols × rows result.
→ Bound i by cols and j by rows for the buffered form.
That copies without flipping — not a transpose.
→ Always use out[i][j] = a[j][i].
Shape changes; there is no same-array home for every entry.
→ Use a second buffer for non-square matrices.
Wrong sizes print garbage or walk past filled data.
→ Keep counts consistent with the initializer.
Fixed buffers must fit both dimensions of both matrices.
→ Ensure MAX ≥ max(rows, cols).
Keep dimensions honest and buffers large enough.
rows and colsThey must match the slice of the array you filled. Wrong counts produce garbage or logic errors.
MAX must be at least max(rows, cols) for both dimensions of both matrices when using one big square buffer.
A 1×n row becomes an n×1 column (and vice versa) with the same rule.
Only for n×n: swap across the diagonal with i < j; still optional for interviews.
Applying transpose twice should recover A exactly.
When printing AT, outer count is cols and inner is rows.
To experiment, change the numbers inside matrix and adjust rows and cols so they stay consistent with how much of the array you actually use.
| Sample | Result highlight |
|---|---|
| 2×3 demo | AT is 3×2 with columns [1,2,3] and [4,5,6] |
| 3×3 demo | AT first column is 1 4 7 |
Try these variations to lock in the pattern.
4 5 6a[i][j] with a[j][i](AT)ij = Aji; sizes swap from m×n to n×m.transposed[i][j] = matrix[j][i] with matching loop bounds.(AT)T = A.Quick Takeaway: flip the shape to n×m, then fill with out[i][j] = a[j][i] — prefer a second buffer until you are ready for in-place square tricks.
| Task | Time | Extra space |
|---|---|---|
Transpose m × n (buffered) | O(m · n) | O(m · n) for the output buffer |
Matrix transpose flips rows into columns: m×n becomes n×m, and each entry moves via out[i][j] = a[j][i]. Master the 2×3 and 3×3 samples so you can reason about buffers and optional in-place square variants later.
Practice both examples above, then continue to finding the maximum value in an array for a 1D warm-up.
Shape flips, then out[i][j] = matrix[j][i] — and (AT)T = A.
m×n → n×m before codingout[i][j] = a[j][i] consistentlyrows/cols consistent with the dataO(m·n) time for the classic passMAX for both dimensionsImplement it the interview-friendly way.
(AT)ij=Aji
Definitionm×n → n×m
FlipCopy out
Safeout[i][j]=a[j][i]
CodeO(m·n)
AnalysisThe transpose flips a matrix across its diagonal: rows become columns. If A is m × n, then AT is n × m, and (AT)T = A.
Learn how to find the largest element in a 1D array with a simple scan in C.
8 people found this page helpful