在 C# 中转置矩阵
矩阵的转置是将矩阵翻转其对角线,这样做将行元素放在列上,而将列元素放在行上。
例如 -
Matrix before Transpose: 123 456 789 Matrix after Transpose: 147 258 369
让我们看看 C# 中的一个示例,以便实现矩阵的转置 -
示例
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("
");
}
}运行上述程序将产生以下结果。在此,必须从用户输入行和列数以及矩阵的元素 -
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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP