c-sharp
  1. c-sharp-program-to-find-transpose-of-a-matrix

Program to Find Transpose of a Matrix - (C# Basic Programs)

Matrix transposition is a common mathematical operation in which the rows and columns of a matrix are switched. A transpose of matrix A is represented by AT where the rows become columns and the columns become rows. In this tutorial, we will discuss a C# program to find the transpose of a matrix.

Syntax

The syntax for finding the transpose of a matrix in C# is as follows:

// Define a 2D array with the original matrix values
int[,] matrix = {
    { 1, 2, 3 },
    { 4, 5, 6 },
    { 7, 8, 9 }
};

// Create a new 2D array to store the transposed matrix
int[,] transposedMatrix = new int[matrix.GetLength(1), matrix.GetLength(0)];

// Iterate through each element in the original matrix
for (int i = 0; i < matrix.GetLength(0); i++) {
    for (int j = 0; j < matrix.GetLength(1); j++) {
        // Assign the transposed value to the new array
        transposedMatrix[j, i] = matrix[i, j];
    }
}

Example

using System;

class TransposeMatrix
{
    static void Main(string[] args)
    {
        int rows, columns;
        Console.Write("Enter the number of rows in the matrix: ");
        rows = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter the number of columns in the matrix: ");
        columns = Convert.ToInt32(Console.ReadLine());

        int[,] matrix = new int[rows, columns];
        Console.WriteLine("Enter the elements of the matrix:");
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                Console.Write("Element [{0},{1}]: ", i, j);
                matrix[i, j] = Convert.ToInt32(Console.ReadLine());
            }
        }

        Console.WriteLine("Original Matrix:");
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                Console.Write(matrix[i, j] + "\t");
            }
            Console.WriteLine();
        }

        int[,] transposedMatrix = new int[columns, rows];
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                transposedMatrix[j, i] = matrix[i, j];
            }
        }

        Console.WriteLine("Transposed Matrix:");
        for (int i = 0; i < columns; i++)
        {
            for (int j = 0; j < rows; j++)
            {
                Console.Write(transposedMatrix[i, j] + "\t");
            }
            Console.WriteLine();
        }
    }
}

Output

Enter the number of rows in the matrix: 3
Enter the number of columns in the matrix: 3
Enter the elements of the matrix:
Element [0,0]: 1
Element [0,1]: 2
Element [0,2]: 3
Element [1,0]: 4
Element [1,1]: 5
Element [1,2]: 6
Element [2,0]: 7
Element [2,1]: 8
Element [2,2]: 9
Original Matrix:
1       2       3
4       5       6
7       8       9
Transposed Matrix:
1       4       7
2       5       8
3       6       9

Explanation

This program first asks the user to input the number of rows and columns in the matrix. It then takes in the matrix elements as input and stores them in the 2D array. After that, it prints the original matrix. Then, the program creates a new 2D array to store the transposed matrix and iterates through each element of the original matrix, and assigns the transposed value to the new array. Finally, it prints the transposed matrix.

Use

Matrix transposition is a common mathematical operation that is used in various applications, such as image processing, signal processing, and linear algebra. This program can be used to find the transpose of any given matrix in C# language.

Summary

In this tutorial, we have discussed a C# program to find the transpose of a matrix. We have seen the syntax, example, explanation, and use of the matrix transposition operation in C#. By understanding the basic concepts of matrix operations, programmers can create more complex programs that involve mathematical operations.

Published on: