- Becomes
- 3×2
Perform Matrix Transpose in C#
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
new int[cols, rows]and nested loops in C#. - A 2×3 reference example (output shape 3×2), a 3×3 square example, plus a live preview.
Overview
Transposing is one of the gentlest matrix operations: you copy each value from position (j, i) in the original into position (i, j) in the result. The grid “flips” so height and width swap.
Two programs
Rectangle 2×3 and square 3×3.
Live preview
Same numbers as example 1, computed in the browser.
Why a copy?
A separate int[,] avoids overwriting values you still need while building the transpose.
Prerequisites
Nested for loops, int[,] syntax, and GetLength. Prior matrix tutorials (addition, subtraction) use the same loop habits.
using System;,staticmethods,Console.Write/WriteLine.- Indexing with
matrix[i, j]— rowi, columnj.GetLength(0)is rows,GetLength(1)is columns.
What is the transpose?
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. The first row of A becomes the first column of AT.
Formal rule
For all valid indices, (AT)ij = Aji. In C# you write transposed[i, j] = matrix[j, i] when i ranges over columns of the original and j over rows — matching the loop bounds below.
For the 2×3 reference matrix, A[0,1] = 2 becomes (AT)[1,0] = 2.
Quick examples
- Row 1
- Becomes column 1
Picture it: tilt the matrix so what was horizontal becomes vertical — or read down the first column of AT and you are reading across the first row of A.
Live preview
Built-in 2×3 sample from example 1. Press the button to see A and AT.
Algorithm
Goal: given matrix with rows × cols, fill transposed with transposed[i,j] = matrix[j,i] where the result is cols × rows.
Read dimensions
rows = matrix.GetLength(0), cols = matrix.GetLength(1).
Allocate swapped shape
int[,] transposed = new int[cols, rows];
Copy with swapped indices
For i in 0 .. cols-1 and j in 0 .. rows-1: transposed[i,j] = matrix[j,i].
📜 Pseudocode
function transpose(matrix): // matrix is rows × cols
rows ← row count of matrix
cols ← column count of matrix
out ← new matrix[cols][rows]
for i from 0 to cols - 1:
for j from 0 to rows - 1:
out[i][j] ← matrix[j][i]
return out Transpose a 2×3 matrix (reference)
Classic beginner layout: a 2×3 literal, Transpose returns a new cols × rows array, and DisplayMatrix prints both the original and the result.
using System;
class Program
{
static int[,] Transpose(int[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
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];
}
}
return transposed;
}
static void DisplayMatrix(string title, int[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
Console.WriteLine(title + $" ({rows} x {cols}):");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Write(matrix[i, j] + "\t");
}
Console.WriteLine();
}
}
static void Main()
{
int[,] matrix = {
{ 1, 2, 3 },
{ 4, 5, 6 }
};
int[,] transposed = Transpose(matrix);
DisplayMatrix("Original", matrix);
Console.WriteLine();
DisplayMatrix("Transposed Matrix", transposed);
}
} Explanation
The assignment transposed[i, j] = matrix[j, i] is the whole idea. Loop ranges follow the swapped dimensions: outer i runs cols times, inner j runs rows times. GetLength removes the need to pass row/column counts separately.
Transpose a 3×3 matrix
Square example: first row 1 2 3 becomes first column of the result. Still uses a separate buffer — simplest and safest for learners.
using System;
class Program
{
static int[,] Transpose(int[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
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];
}
}
return transposed;
}
static void DisplayMatrix(string title, int[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
Console.WriteLine(title);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
}
static void Main()
{
int[,] a = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
int[,] t = Transpose(a);
DisplayMatrix("A", a);
Console.WriteLine();
DisplayMatrix("A^T", t);
}
} Explanation
For square matrices, output shape matches input shape. In-place transpose (without extra memory) is possible for squares but easier to get wrong; two buffers keep the lesson clear.
Beyond the basics
In place (square only). Swap a[i,j] with a[j,i] for i < j to avoid a second matrix; watch indexing carefully.
Generic helper. Return int[,] from Transpose and reuse DisplayMatrix for any rectangular int[,] — same pattern as addition and subtraction helpers.
Interview: state the size swap rule, write transposed[i,j] = matrix[j,i], and mention why a copy is needed for non-square matrices.
❓ FAQ
🔄 Input / output examples
Change the literals in Main to experiment. GetLength picks up the new shape automatically.
| Original A | Position | In AT |
|---|---|---|
1 (2×3, row 0 col 0) | A[0,0] | (AT)[0,0] = 1 |
5 (2×3, row 1 col 1) | A[1,1] | (AT)[1,1] = 5 |
2 (2×3, row 0 col 1) | A[0,1] | (AT)[1,0] = 2 |
4 (3×3, row 1 col 0) | A[1,0] | (AT)[0,1] = 4 |
Edge cases and pitfalls
Most bugs come from wrong loop bounds or trying to transpose in place when the shape changes.
Non-square matrices
A 2×3 becomes 3×2 — you must allocate new int[cols, rows], not reuse the original size.
Swapped indices
transposed[i,j] = matrix[j,i] — not matrix[i,j] on the right. The loop bounds swap too.
Single cell
Still valid — one assignment copies the only element; shape stays 1×1.
Square only
In-place diagonal swaps work for n×n but are trickier; a second buffer is the safe teaching approach.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
Transpose m × n | O(m · n) | O(m · n) for the output buffer |
DisplayMatrix | O(m · n) | O(1) |
Every element is copied once — optimal for a straightforward transpose with a separate result array.
Summary
- Rule:
(AT)ij = Aji; sizes swap fromm×nton×m. - C#:
new int[cols, rows],transposed[i,j] = matrix[j,i],GetLengthfor bounds. - Double transpose:
(AT)T = A.
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
