从随机创建的4×4二进制矩阵的初始猜测开始,编写一个代码片段,执行以下100次迭代:
打印最后的4×4矩阵和在100次迭代结束时找到的行列式的值。
import numpy as np
MOld = np.random.randint(2, size=[4,4])
for j in range(100): #for loop over 100 iterations
MNew = np.array(MOld) #new matrix equal to old matrix
i,j = np.random.randint(4), np.random.randint(4) #choosing random elements of the matrix.
MNew[i,j] = 1 - MNew[i,j] #do not understand this
if f(MNew) < f(MOld): #if new matrix < old matrix
MOld = MNew #replacing value
print(MOld) #printing original 4x4 matrix
print(f(MOld)) #printing determinant value我正在努力提高我对这段代码的理解,如果有人能在标签#之后查看我的评论,我将不胜感激。
我特别不明白这一步:
MNewi,j=1- MNewi,j
谢谢你提前提供帮助。
发布于 2019-03-10 19:47:20
步骤:
如果MNewi,j是1,那么MNewi,j现在是1-1= 0.
如果MNewi,j是0,那么Mnewi,j现在是1-0=1。
因此,您可以看到,这是一种从上一次迭代中翻转值的方法。
https://stackoverflow.com/questions/55091635
复制相似问题