I have an array:
a = np.array([2,3,5,8,3,5])
What is the most efficient (vectorized) way to calculate an array where each resulting element is (Pseudocode):
result[0] = a[0]
for i > 0:
result[i] = result[i-1] + (a[i] - result[i-1]) * factor
I could do this with the following inefficient code (factor = 0.5):
a = np.array([2,3,5,8,3,5])
result = np.array([a[0]])
for k in a[1:]:
result = np.append(result, result[-1]+(k-result[-1])*0.5)
The result of this damping function would be:
array([ 2., 2.5, 3.75, 5.875, 4.4375, 4.71875])