
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Transpose a matrix in C#
Transpose of a matrix flips the matrix over its diagonal and this brings the row elements on the column and column elements on the row.
For example −
Matrix before Transpose: 123 456 789 Matrix after Transpose: 147 258 369
Let us see an example in C# to achieve transpose of a matrix −
Example
using System; public class Demo { public static void Main() { int i, j, m, n; int[, ] arr1 = new int[30, 30]; int[, ] arr2 = new int[30, 30]; Console.Write("
Enter the number of rows and columns of the matrix :
"); Console.Write("Rows entered = "); m = Convert.ToInt32(Console.ReadLine()); Console.Write("Columns entered = "); n = Convert.ToInt32(Console.ReadLine()); Console.Write("Set elements in the matrix...
"); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { Console.Write("
[{0}],[{1}] : ", i, j); arr1[i, j] = Convert.ToInt32(Console.ReadLine()); } } Console.Write("
Matrix before Transpose:
"); for (i = 0; i < m; i++) { Console.Write("
"); for (j = 0; j < n; j++) Console.Write("{0}\t", arr1[i, j]); } for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { arr2[j, i] = arr1[i, j]; } } Console.Write("
Matrix after Transpose: "); for (i = 0; i < m; i++) { Console.Write("
"); for (j = 0; j < n; j++) { Console.Write("{0}\t", arr2[i, j]); } } Console.Write("
"); } }
The following result will be produced on running the above program. Here, values from the user are to be entered for number of rows and columns, and the elements of the matrix −
Enter the number of rows and columns of the matrix :3 3 Rows entered = 3 Columns entered 3 Set elements in the matrix... [0],[0] : 1 [0],[1] : 2 [0],[2] : 3 [1],[0] : 4 [1],[1] : 5 [1],[2] : 6 [2],[0] : 7 [2],[1] : 8 [2],[2] : 9 Matrix before Transpose: 123 456 789 Matrix after Transpose: 147 258 369
- Related Articles
- Transpose a matrix in Java
- Transpose a matrix in Python?
- Java program to transpose a matrix.
- How to Transpose a Matrix using Python?
- Find the transpose of a matrix in Python Program
- C++ Program to Find Transpose of a Matrix
- Java Program to Find Transpose of a Matrix
- How to Transpose a matrix in Single line in Python?
- Compute a matrix transpose with Einstein summation convention in Python
- Java program to print the transpose of a matrix
- Python Program to find the transpose of a matrix
- C++ Program to Find Transpose of a Graph Matrix
- Golang Program To Find The Transpose Of A Matrix
- How to calculate transpose of a matrix using C program?
- Program to find the transpose of given matrix in Python

Advertisements