我正在尝试制作一个程序,计算从矩阵开始到矩阵中心所需的步骤数,在这个过程中,我遇到了这个错误,代码:
import numpy as np
matrix = np.zeros([5,5])
matrix[0][0] = 1
count = 0
while matrix[2][2] != 1:
count += 1
matrix[0][0] = matrix[count][count]
print(count)谢谢!
发布于 2021-06-08 13:39:58
matrix[0][0] = matrix[count][count]你把它搞错了。矩阵已为1,但随后设置为0。
你想:
matrix[count][count] = matrix[0][0]发布于 2021-06-08 13:42:41
我想你是在给矩阵赋值,这就是为什么循环永远不会结束,并且离开数组索引
import numpy as np
matrix = np.zeros([5,5])
matrix[0][0] = 1
count = 0
while matrix[2][2] != 1:
count += 1
matrix[count][count] = matrix[0][0]
print(count)https://stackoverflow.com/questions/67888108
复制相似问题