我的代码基本上像这样
P = matrix # initialise value of P matrix
x = some other matrix
for i, val in enumerate(vals):
lots of matrix calculations involving P and x
y = z - j # this is the important line
lots of matrix calculations UPDATING P and x
return values of P and x for each step
现在,我想更改代码,以便如果y大于某个阈值,例如y> 0.5,我将p和x重置为它们的初始值 - 然后再次继续循环,就好像它只是从头开始启动一样。我不确定最好的方法,我是Python的新手在功能中。
欢呼
s
python list
中是一个可变的对象。要在计算中间从头开始,请使用原始值保持原始列表和重新分配P和X的深度副本,然后继续。这样的东西:
import copy
P = matrix # initialise value of P matrix
x = some other matrix
p1= copy.deepcopy(P)
x1= copy.deepcopy(x)
for i, val in enumerate(vals):
lots of matrix calculations involving P and x
y = z - j # this is the important line
if y > threshold:
P = p1
x = x1
continue
lots of matrix calculations UPDATING P and x
return values of P and x for each step