Prerequisite: Linear Algebra | Defining a Matrix
先決條件: 線性代數| 定義矩陣
In the python code, we will add two Matrices. We can add two Matrices only and only if both the matrices have the same dimensions. Therefore, knowing the dimensions of the matrices turns out to be one of the major steps in Linear Algebra. The following code shows how an inbuilt function can be used to achieve our goal of the shape of a Matrix.
在python代碼中,我們將添加兩個矩陣。 僅當兩個矩陣的維數相同時,我們才可以添加兩個矩陣。 因此,了解矩陣的維數成為線性代數的主要步驟之一。 以下代碼展示了如何使用內置函數來實現矩陣形狀的目標。
查找矩陣形狀的Python代碼 (Python code for fidning Shape of Matrix)
# Linear Algebra Learning Sequence
# Shape of Matrix
import numpy as np
#Use of np.array() to define a matrix
V1 = np.array([[1,2,3],[2,3,5],[3,6,8],[323,623,823]])
V2 = np.array([[965,2413,78,44],[223,356,500,44],[312,66,78,44],[42,42,42,44],[44,44,44,44]])
print("--The Matrixa A-- \n",V1)
print("\n--The Matrix B-- \n",V2)
print("\n\n Shape of the matrix A: ", V1.shape)
print(" Shape of the matrix B: ", V2.shape)
Output:
輸出:
--The Matrixa A--
[[ 1 2 3]
[ 2 3 5]
[ 3 6 8]
[323 623 823]]
--The Matrix B--
[[ 965 2413 78 44]
[ 223 356 500 44]
[ 312 66 78 44]
[ 42 42 42 44]
[ 44 44 44 44]]
Shape of the matrix A: (4, 3)
Shape of the matrix B: (5, 4)
翻譯自: https://www.includehelp.com/python/shape-of-matrix.aspx