【问题标题】:python ndarray update exiting functionpython ndarray更新退出函数
【发布时间】:2016-08-30 13:32:36
【问题描述】:

我在函数定义中有以下循环:

mu=np.zeros((T,p))
mu_post=np.zeros((T,p))

for t in arange(T):
      mu_post[t]= np.dot(a,b)
      mu[t+1]= mu_post[t]
      some other processing

在接近循环结束时(t=T-1),代码退出此函数而不执行它。但是,在尝试调试时,如果我手动(在 pycharm 调试控制台中)尝试执行 mu[t+1]= mu_post[t],它可以正常工作。这可能是有原因的。顺便说一句,我试过做 mu[t+1][:]= mu_post[t] 但它有同样的问题。

【问题讨论】:

    标签: python arrays loops numpy assignment-operator


    【解决方案1】:

    代码退出这个函数而不执行它。

    这是模糊的,但我怀疑我可以用这个重现错误:

    In [47]: mu=np.zeros((5,3))
    In [48]: mu1=np.zeros((5,3))
    
    In [49]: for t in range(5):
       ....:     mu1[t,:]=[1,2,3]
       ....:     mu[t+1,:] = mu1[t]
    
    ----> 3     mu[t+1,:] = mu1[t]
    
    IndexError: index 5 is out of bounds for axis 0 with size 5
    

    在最后一次迭代中,t==4,所以t+15。对于mu 的第一个维度来说,这个索引太大了。

    为什么要使用t+1 索引mu?如果这是有道理的,为什么mumu1 大小相同?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多