In the python code, we will build a matrix using rows. We can stack two arrays to form a matrix only and only if both the arrays/rows have the same dimensions. The following code shows how an inbuilt vstack() function can be used to stack three rows to form a matrix.
在python代码中,我们将使用行来构建矩阵。 仅当两个数组/行的尺寸相同时,我们才可以堆叠两个数组以形成矩阵。 以下代码显示了如何使用内置的vstack()函数来堆叠三行以形成矩阵。
使用行创建矩阵的Python代码 (Python code for creating a matrix using rows)
# Creating a Matrix using Rows
import numpy as np
#Use of np.array() to define rows
V1 = np.array([45,78,65,99])
V2 = np.array([68,87,97,48])
V3 = np.array([74,68,77,48])
print("The Row v1: ",V1)
print("The Row v2: ",V2)
print("The Row v3: ",V3)
#Making a Matrix using Vectors
M = np.vstack([V1,V2,V3])
print("\n\n -----Matrix M---\n", M)
print("\n\nShape of the matrix B: ", M.shape)
Output:
输出:
The Row v1: [45 78 65 99]
The Row v2: [68 87 97 48]
The Row v3: [74 68 77 48]
-----Matrix M---
[[45 78 65 99]
[68 87 97 48]
[74 68 77 48]]
Shape of the matrix B: (3, 4)
翻译自: https://www.includehelp.com/python/creating-a-matrix-using-rows.aspx