Numpy: calculate based on previous element?

后端 未结 5 2283
北恋
北恋 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:45

    Here is how you do it with Python:

    x = [1, 2, 3, 4, 5, 6, 7, 8 ,9, 10]
    y = [50]
    for i in range(len(x)):
        y.append(y[-1] * 2 + x[i])
    y = y[1:]
    

    You might want to calculate it from the last element to prevent using the new values for the next i.

提交回复
热议问题