I was wondering if there was a vertorized way to use two arrays and based on the value of one continuously copy the value in a second array until a new value in the first array is found and then repeat the whole process.
here is an example
a = np.array([FALSE,TRUE,FALSE,FALSE, FALSE, TRUE, FALSE FALSE})
b = np.array([0,10,0,0,0,20,0,0,])
output = array([0,10,10,10,10,20,20,20])
I can use a loop which is much too slow for this.
for i in range(len(b)):
if b[i-1] and not b[i]:
b[i] = b[i-1]
Updated:
I tried this code below and increased the speed by 5x but I figure there should be a faster more elegant way of doing this.
import numpy as np
nz= np.concatenate((np.nonzero(b)[0], [len(b)]))
np.repeat(b[np.nonzero(b)], np.diff(nz))
a?ain this example