Example:
a = np.array([1, 0, 2, 0, 3, 4])
Goal:
Set value to 0 if the previous value is 0
Desired output:
[1, 0, 0, 0, 0, 4]
Example:
a = np.array([1, 0, 2, 0, 3, 4])
Goal:
Set value to 0 if the previous value is 0
Desired output:
[1, 0, 0, 0, 0, 4]
Make a mask of the locations that are zero:
m = (a == 0)
Apply the mask to a shifted slice of the array:
a[1:][m[:-1]] = 0
In some cases, you may want to shift by incrementing the indices:
i = np.flatnonzero(m[:-1]) + 1
a[i] = 0