Numpy: calculate based on previous element?

后端 未结 5 2419
北恋
北恋 2021-02-08 15:45

Say that I have array x and y:

x = numpy.array([1,2,3,4,5,6,7,8,9,10])  # actual content is the a result of another calculation step
         


        
5条回答
  •  轮回少年
    2021-02-08 16:26

    This is how to do it with numpy:

    import numpy as np
    x = np.array([ 1, 2, 3, 4, 5, 6, 7, 8 ,9, 10 ])
    y = np.array([ 50 ])
    for i in np.arange(len(x)):
        y = np.append(
                      y,
                      ( y[-1] * 2 + x[i] )
                      )
    y = y[1:]
    
    print(y)
    

提交回复
热议问题