如何在python中表示矩阵

Bun*_*bit 25 python matrix

我如何在python中表示矩阵?

Gyö*_*sek 44

看看这个答案:

from numpy import matrix
from numpy import linalg
A = matrix( [[1,2,3],[11,12,13],[21,22,23]]) # Creates a matrix.
x = matrix( [[1],[2],[3]] )                  # Creates a matrix (like a column vector).
y = matrix( [[1,2,3]] )                      # Creates a matrix (like a row vector).
print A.T                                    # Transpose of A.
print A*x                                    # Matrix multiplication of A and x.
print A.I                                    # Inverse of A.
print linalg.solve(A, x)     # Solve the linear equation system.
Run Code Online (Sandbox Code Playgroud)


Ed.*_*Ed. 12

Python没有矩阵.您可以使用列表列表或NumPy


小智 5

如果您不打算使用NumPy库,则可以使用嵌套列表。这是实现动态嵌套列表(二维列表)的代码。

r行数

let r=3

m=[]
for i in range(r):
    m.append([int(x) for x in raw_input().split()])
Run Code Online (Sandbox Code Playgroud)

任何时候您都可以使用

m.append([int(x) for x in raw_input().split()])
Run Code Online (Sandbox Code Playgroud)

在上面,您必须按行输入矩阵。要插入列:

for i in m:
    i.append(x) # x is the value to be added in column
Run Code Online (Sandbox Code Playgroud)

要打印矩阵:

print m       # all in single row

for i in m:
    print i   # each row in a different line
Run Code Online (Sandbox Code Playgroud)