Transpose a matrix in C#

Transpose of a matrix flips the matrix over its diagonal, swapping rows and columns. This operation converts the element at position [i][j] to position [j][i] in the transposed matrix.

For example −

Matrix before Transpose:
1 2 3
4 5 6
7 8 9

Matrix after Transpose:
1 4 7
2 5 8
3 6 9

Matrix Transpose Operation Original Matrix 1 2 3 4 5 6 7 8 9 rows columns transpose Transposed Matrix 1 4 7 2 5 8 3 6 9 rows columns Element [i][j] becomes [j][i]

Syntax

Following is the basic approach for matrix transpose −

// For transpose: transposed[j][i] = original[i][j]
for (int i = 0; i < rows; i++) {
    for (int j = 0; j < columns; j++) {
        transposed[j, i] = original[i, j];
    }
}

Using a Simple Transpose Method

Example

using System;

public class MatrixTranspose {
    public static void Main() {
        int[,] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);
        
        Console.WriteLine("Original Matrix:");
        PrintMatrix(matrix, rows, cols);
        
        int[,] transposed = new int[cols, rows];
        
        // Transpose operation
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                transposed[j, i] = matrix[i, j];
            }
        }
        
        Console.WriteLine("\nTransposed Matrix:");
        PrintMatrix(transposed, cols, rows);
    }
    
    static void PrintMatrix(int[,] matrix, int rows, int cols) {
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                Console.Write(matrix[i, j] + "\t");
            }
            Console.WriteLine();
        }
    }
}

The output of the above code is −

Original Matrix:
1       2       3
4       5       6
7       8       9

Transposed Matrix:
1       4       7
2       5       8
3       6       9

Using In-Place Transpose for Square Matrices

For square matrices, we can perform transpose without using extra space by swapping elements across the diagonal −

Example

using System;

public class InPlaceTranspose {
    public static void Main() {
        int[,] matrix = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 16}
        };
        
        int size = matrix.GetLength(0);
        
        Console.WriteLine("Original Matrix:");
        PrintMatrix(matrix, size);
        
        // In-place transpose for square matrix
        for (int i = 0; i < size; i++) {
            for (int j = i + 1; j < size; j++) {
                // Swap elements across diagonal
                int temp = matrix[i, j];
                matrix[i, j] = matrix[j, i];
                matrix[j, i] = temp;
            }
        }
        
        Console.WriteLine("\nTransposed Matrix:");
        PrintMatrix(matrix, size);
    }
    
    static void PrintMatrix(int[,] matrix, int size) {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                Console.Write(matrix[i, j] + "\t");
            }
            Console.WriteLine();
        }
    }
}

The output of the above code is −

Original Matrix:
1       2       3       4
5       6       7       8
9       10      11      12
13      14      15      16

Transposed Matrix:
1       5       9       13
2       6       10      14
3       7       11      15
4       8       12      16

Using LINQ for Matrix Transpose

You can also transpose a matrix using LINQ for a more functional approach −

Example

using System;
using System.Linq;

public class LinqTranspose {
    public static void Main() {
        int[][] matrix = {
            new int[] {1, 2, 3},
            new int[] {4, 5, 6}
        };
        
        Console.WriteLine("Original Matrix:");
        PrintJaggedMatrix(matrix);
        
        // Transpose using LINQ
        var transposed = matrix
            .SelectMany(row => row.Select((value, index) => new { value, index }))
            .GroupBy(x => x.index, x => x.value)
            .Select(g => g.ToArray())
            .ToArray();
        
        Console.WriteLine("\nTransposed Matrix:");
        PrintJaggedMatrix(transposed);
    }
    
    static void PrintJaggedMatrix(int[][] matrix) {
        foreach (var row in matrix) {
            foreach (var element in row) {
                Console.Write(element + "\t");
            }
            Console.WriteLine();
        }
    }
}

The output of the above code is −

Original Matrix:
1       2       3
4       5       6

Transposed Matrix:
1       4
2       5
3       6

Comparison

Method Space Complexity Best For
Simple Transpose O(m×n) Any matrix dimensions
In-Place Transpose O(1) Square matrices only
LINQ Transpose O(m×n) Functional programming style

Conclusion

Matrix transpose in C# can be implemented using different approaches depending on your requirements. Use the simple method for general cases, in-place transpose for memory-efficient operations on square matrices, and LINQ for functional programming style when working with jagged arrays.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements