To find the transpose of a matrix in C#, you can use a nested loop to iterate through the rows and columns of the matrix and swap the elements. Here is an example of how to do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | using System; namespace ConsoleApplication { class Program { static void Main(string[] args) { // Create a matrix int[,] matrix = new int[3, 3] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // Find the transpose of the matrix int[,] transpose = Transpose(matrix); // Print the transpose to the console for (int i = 0; i < transpose.GetLength(0); i++) { for (int j = 0; j < transpose.GetLength(1); j++) { Console.Write(transpose[i, j] + " "); } Console.WriteLine(); } } static int[,] Transpose(int[,] matrix) { // Create a new matrix with the transposed dimensions int[,] transpose = new int[matrix.GetLength(1), matrix.GetLength(0)]; // Iterate through the rows and columns of the matrix for (int i = 0; i < matrix.GetLength(0); i++) { for (int j = 0; j |
This C# program demonstrates how to find the transpose of a matrix. The transpose of a matrix is a new matrix that is obtained by swapping the rows and columns of the original matrix.
The program defines a Transpose
method that takes a matrix as input and returns the transpose of the matrix. The method creates a new matrix with the transposed dimensions and then uses a nested loop to iterate through the rows and columns of the original matrix. For each element of the original matrix, the method assigns the value of the element to the corresponding element in the transpose matrix.
In the Main
method, the program creates a 3×3 matrix and then calls the Transpose
method to find the transpose of the matrix. It then prints the transpose to the console.
When you run the program, it will output the following transposed matrix:
1 2 3 4 5 | 1 4 7 2 5 8 3 6 9 |