python回歸之旅-用python學(xué)習(xí)數(shù)學(xué)---2023-019
這次總算找到了numpy里面矩陣的輸入方法(2種),測試沒有報錯。(這個是真心不容易,不過input沒有提示阿)
import numpy as np
a=[]
r=input("please input the row number of a:")
ele=input("please input the element number of a row in a:")
for i in range(int(r)):
? ? row=[]
? ? for j in range(int(ele)):
? ? ? ? element=float(input())
? ? ? ? row.append(element)
? ? a.append(row)
print(a)
--------------------------------------------------------------
第一種是先確定矩陣維數(shù),然后一個一個輸入。
b=[]
r1=input("please input the row number of b:")
ele1=input("please input the element number of a row in b:")
for i in range(int(r)):
? ? row=list(map(float,input().split()))
? ? b.append(row)
print(b)
-------------------------------------------------------------
第二種是確定矩陣維數(shù),然后按照行輸入。(關(guān)鍵可以定義一下輸入信息的類型,免得后面報錯)
l1=np.shape(a)
l2=np.shape(b)
if l1==l2:
? ? c=np.add(a,b)
? ??
print(c)
---------------------------------------------
之后是判斷矩陣是否可以相加,再做矩陣加法。