在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("\nEnter the number of rows and columns of the matrix :\n");
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...\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
Console.Write("\n [{0}],[{1}] : ", i, j);
arr1[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.Write("\n\nMatrix before Transpose:\n");
for (i = 0; i < m; i++) {
Console.Write("\n");
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("\n\nMatrix after Transpose: ");
for (i = 0; i < m; i++) {
Console.Write("\n");
for (j = 0; j < n; j++) {
Console.Write("{0}\t", arr2[i, j]);
}
}
Console.Write("\n\n");
}
}运行以上程序将产生以下结果。在这里,将输入用户的值,以获取行数和列数以及矩阵的元素-
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
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿