我正在尝试创建一个矩阵函数:def create_matrix(*NumList, rows, cols)。
def create_matrix(*NumList, rows, cols):
Fmat = []
if len(NumList) == rows*cols:
for i in range(rows):
Imat = []
for j in range(cols):
Imat.append(NumList[rows * i + j])
Fmat.append(Imat)
return Fmat
else:
print("The number of elememnts does not match the shape of the matrix.")
对于 create_matrix(*range(4, 19), rows=3, cols=5),期望的输出应该是:
[[4, 5, 6, 7, 8], [9, 10, 11, 12, 13], [14, 15, 16, 17, 18]].
但是,我只能生成以下结果。任何解决方案,谢谢!
[[4, 5, 6, 7, 8], [7, 8, 9, 10, 11], [10, 11, 12, 13, 14]]