Python中用户定义的矩阵计算程序输入

时间:2014-11-21 00:23:07

标签: python matrix input

我已经写下了我的最终目标的“硬编码”版本。正如您所看到的,我有一个特定的n值以及预定义的矩阵X和Y.程序当前执行的计算很好,但我遇到的问题是修改它以接受n的用户定义输入, X和Y根据用户输入的内容准确执行计算。我仍然习惯于Python和用户输入,所以任何编码的帮助将非常感谢!我还应该注意到,我试图在没有NumPy的情况下进行学习。谢谢!

# 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)

2 个答案:

答案 0 :(得分: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 :(得分:-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##################')