I have a np array, let's say
[1, 1, 1, 1, 0, 1, 0, 1, 0]
Now I want to modify the number at index i s.t. it is 0, if one of the previous n elements was a 1. But this calculation should not be static w.r.t to the original array, but dynamic, i.e. considering already changed values. Let's say n=2, then I want to get
[1, 0, 0, 1, 0, 0, 0, 1, 0]
How do I do that efficiently without a for loop? Simple indexing will run over the whole array in one iteration, ignoring newly changed values, meaning the result would be
[1, 0, 0, 0, 0, 0, 0, 0, 0]
right?
Update: In a loop I would do this:
for i in range(a.shape[0]):
FoundPrevious = False
for j in range(n):
if i - j < 0:
continue
if a[i - j] == 1:
FoundPrevious = True
break
if FoundPrevious:
a[i] = 0
[1, 0, 0, 1, 0, 0, 0, 1, 0]?