Pythonでのn*nのマトリックス作成
次元n*nの行列を作成する必要がある場合は、リスト内包表記が使用されます。
以下は同じのデモンストレーションです-
例
N = 4
print("The value of N is ")
print(N)
my_result = [list(range(1 + N * i, 1 + N * (i + 1)))
for i in range(N)]
print("The matrix of dimension N * 0 is :")
print(my_result) 出力
The value of N is 4 The matrix of dimension N * 0 is : [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
説明
-
Nの値は事前定義されています。
-
コンソールに表示されます。
-
行列の次元について説明します。
-
番号が繰り返され、リストに変換されます。
-
これは変数に割り当てられます。
-
コンソールに表示されます。
-
2つの行列のPythonプログラムの追加
与えられた2つのユーザー入力マトリックス。私たちのタスクは、2つの行列の加算を表示することです。これらの問題では、ネストされたリストを包括的に使用します。 アルゴリズム Step1: input two matrix. Step 2: nested for loops only to iterate through each row and columns. Step 3: At each iterationshall add the corresponding elements from two matrices and shall store the result. サンプルコード
-
2つの行列のPythonプログラム乗算。
与えられた2つのユーザー入力マトリックス。私たちのタスクは、2つの行列の加算を表示することです。これらの問題では、ネストされたリストを包括的に使用します。 アルゴリズム Step1: input two matrix. Step 2: nested for loops to iterate through each row and each column. Step 3: take one resultant matrix which is initially contains all 0. Then we multiply each row elements of first matrix