dec = 0.1
data = np.array([100,200,300,400,500])
I have a for loop like this
y = np.zeros(len(data))
for i in range(len(data)):
if i == 0:
y[i] = (1.0 - dec) * data[i]
else:
y[i] = (1.0 - dec) * data[i] + (dec * y[i - 1])
Output y is:
array([ 90. , 189. , 288.9 , 388.89 , 488.889])
And now I want to do the above calculation without a loop, so if I break the code and do
data[0] = (1.0 - dec) * data[0]
data[1:] = (1.0 - dec) * data[1:] + (dec * data[0])
Output data is:
array([ 90, 189, 279, 369, 459])
When you compare y and data output first two values are correct because it is getting multiplied with data[0] which makes sense but later on it should continue as the loop does in loop code, so how can we achieve that? Is there a function that can handle this? This is mainly to optimize my code so that it runs faster for thousands of data.
The expected output is the same as the y output.