Perform Matrix Transpose in C#

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 2 Code Examples
2D arrays

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;, static methods, Console.Write / WriteLine.
  • Indexing with matrix[i, j] — row i, column j. 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.

Shape m×n → n×m
Flip twice (AT)T = A
Entry swap i and j

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.

Sample cell

For the 2×3 reference matrix, A[0,1] = 2 becomes (AT)[1,0] = 2.

Quick examples

2×3 Reference
Becomes
3×2
3×3 Square
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.

Matches the first C# program’s numbers.

Live result
Press “Transpose sample”.

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

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
1

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.

c#
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.

2

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.

c#
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

Take every row of the original matrix and write it as a column of a new matrix — swap row and column indices 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 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.
rows = matrix.GetLength(0), cols = matrix.GetLength(1). The transpose is new int[cols, rows].
You touch every element once: O(m·n) time for an m×n matrix, with O(m·n) space for the output array.

🔄 Input / output examples

Change the literals in Main to experiment. GetLength picks up the new shape automatically.

Original APositionIn 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.

Shape

Non-square matrices

A 2×3 becomes 3×2 — you must allocate new int[cols, rows], not reuse the original size.

Index

Swapped indices

transposed[i,j] = matrix[j,i] — not matrix[i,j] on the right. The loop bounds swap too.

1×1

Single cell

Still valid — one assignment copies the only element; shape stays 1×1.

In place

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

TaskTimeExtra space
Transpose m × nO(m · n)O(m · n) for the output buffer
DisplayMatrixO(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 from m×n to n×m.
  • C#: new int[cols, rows], transposed[i,j] = matrix[j,i], GetLength for bounds.
  • Double transpose: (AT)T = A.
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.

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.

8 people found this page helpful