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
-
C#のコンソールクラス
C#のConsoleクラスは、コンソールアプリケーションの標準の入力、出力、およびエラーストリームを表すために使用されます。 C#のコンソールクラスプロパティの例をいくつか見てみましょう- Console.CursorLeftプロパティ C#でコンソールのCursorLeftを変更するには、Console.CursorLeftプロパティを使用します。 例 例を見てみましょう- using System; class Demo { public static void Main (string[] args) { Cons
-
行列の転置を見つけるPythonプログラム
この記事では、特定の問題ステートメントを解決するための解決策とアプローチについて学習します。 問題の説明 行列が与えられた場合、転置を同じ行列に格納して表示する必要があります。 行列の転置は、行を列に、列を行に変更することで得られます。つまり、A行列の転置はA[i][j]をA[j][i]に変更することで得られます。 以下に示す実装を見てみましょう- 例 N = 4 def transpose(A): for i in range(N): for j in range(i+1, N): &nbs