2014-11-21 75 views
0

我已經寫下了我的最終目標的「硬編碼」版本。正如你所看到的,對於n以及預定義的矩陣X和Y,我有一個特定的值。程序當前正在執行計算,但是我遇到的麻煩是將其修改爲接受用戶爲n定義的輸入, X和Y,並根據用戶輸入的內容準確執行計算。我仍然習慣Python和用戶輸入,所以任何編程幫助將不勝感激!我也應該注意到,我正在努力做到這一點沒有NumPy的學習目的。謝謝!Python中用於矩陣計算程序的用戶定義輸入

# Program to add two matrices 
# using nested loop 

n = 3 

X = [[12,7,3], 
    [4 ,5,6], 
    [7 ,8,9]] 

Y = [[5,8,1], 
    [6,7,3], 
    [4,5,9]] 

result = [[0,0,0], 
     [0,0,0], 
     [0,0,0]] 

# adds the matrices 
# iterate through rows 
for i in range(len(X)): 
    # iterate through columns 
    for j in range(len(X[0])): 
     result[i][j] = X[i][j] + Y[i][j] 

for r in result: 
    print(r) 


# subtracts the matrices 
for i in range(len(X)): 
    for j in range(len(X[0])): 
     result[i][j] = X[i][j] - Y[i][j] 

for r in result: 
    print(r) 

# multiplies the matrices 
for i in range(len(X)): 
    # iterate through columns 
    for j in range(len(X[0])): 
     result[i][j] = sum((X[i][v]*Y[v][j] for v in range(n))) 

for r in result: 
    print(r) 
+0

「有問題就是修改它接受用戶定義的輸入..」有什麼問題嗎?任何代碼示例,錯誤? – Marcin 2014-11-21 01:12:09

+0

沒有錯誤。就像我說的,上面的代碼運行良好。我已經給出了預定義的輸入,我希望它向用戶提出輸入並使用它。 – Ben 2014-11-21 01:14:48

回答

0

也許這會有所幫助。首先你得到一個用戶的n,並且讀取X個矩陣的n行。每行中的值用逗號分隔。

n = int(input("Provide n: ")) 

X = []; 

for rowi in range(n): 
    row_list = list(map(float, input("row {}: ".format(rowi+1)).split(','))) 
    X.append(row_list) 

print(X) 

請注意,這是在Python 3.4測試,並有沒有檢查這裏的任何錯誤。所以可能你需要添加一些條件來檢查用戶輸入是否是數字,例如不是字符串,並且每行有相同數量的條目等。

-1

因爲我們知道matrx可以表示爲Python中的嵌套列表,我們可以提示每個元素的用戶輸入。同一行中的元素首先被包含在名爲row []的列表中,然後它被附加到容器列表中,因此我們得到容器列表中的列表,因此得到矩陣。

#Prints the sum of matrix of orders mxn 
first_row_count = int(input('Enter the number of rows of 1st matrix :')) 
first_col_count = int(input('Enter the number of columns of 1st matrix :')) 

second_row_count = int(input('Enter the number of rows of 1st matrix :')) 
second_col_count = int(input('Enter the number of columns of 1st matrix :')) 

first_Matrix =[] 
second_matrix=[] 

if(first_row_count == second_row_count) and (second_row_count == second_col_count): 
    print('############Enter the elements of first Matrix#################') 
    for i in range(0,first_row_count): 
     row = [] 
     input_variable = None 
     for j in range(0,first_col_count): 
      input_variable = int(input('Enter the element at mat[{0}][{1}]'.format(i,j))) 
      row.append(input_variable) 
     first_Matrix.append(row) 

    print('############Enter the elements of second Matrix#################')  
    for i in range(0,second_row_count): 
     row = [] 
     input_variable = None 
     for j in range(0,second_col_count): 
      input_variable = int(input('Enter the element at mat[{0}][{1}]'.format(i,j))) 
      row.append(input_variable) 
     second_matrix.append(row)   

    print('############Resultant Matrix#################') 
    result = [[first_Matrix[i][j] + second_matrix[i][j] for j in range(len(first_Matrix[0]))] for i in range(len(first_Matrix))] 
    for r in result: 
      print(r) 
else: 
    print('------------The matrix of given orders are not compatible-----------------')  
print('########The Matrix Sum performed##################')  
+0

請不要簡單地發佈一些代碼,但解釋你認爲的解決方案是什麼,也只發布他的代碼部分,其中包含您的描述的實際答案或解決方案:) – Florian 2018-01-07 17:13:02

+0

@Florian我已經添加了邏輯背後執行他以上代碼。 – 2018-01-08 02:08:16