我试图创建两个矩阵,让用户决定尺寸,然后在一个一个输入数字或让系统用随机数字填充之间进行选择。之后,程序应该将两个矩阵相乘并打印结果。在
到目前为止,我得到的是:import numpy as np
print("Choose 4 numbers: ")
#Dimensions for the first matrix
m = int(input("m: "))
n = int(input("n: "))
#Dimensions for the second matrix
m2 = int(input("m2: "))
n2 = int(input("n2: "))
mat = [0]
mat=[(mat*n)]*m
op = int(input("How would you like to arrange your matrix? For number by number press 1, \
for random matrix press 2: "))
if op == 1:
for i in range(m):
for j in range(n):
num = int(input("Choose a number: "))
mat[i][j] = num
print (mat)
for x in range(m2):
for y in range(n2):
num2 = int(input("Choose a number: "))
mat[x][y] = num2
print (mat)
我有几个问题。首先,第一个矩阵是线性形式(例如:“[[1,2,3],[4,5,6],[7,8,9]]”)。其次,第二个矩阵似乎有一个错误:“索引器错误:列表赋值索引超出范围”。对于随机数填充,我知道我可以使用np矩阵以及np.随机. 在
提前谢谢你!在