I have to perform the operation below many times. Using numpy functions instead of loops I usually get a very good performance but I have not been able to replicate this for higher dimensional arrays. Any suggestion or alternative would be most welcome:
I have a boolean array and I would like to propagate the true indeces to the next 2 positions for example:
If this 1 dimensional array (A) is:
import numpy as np
# Number of positions to propagate the array
propagation = 2
# Original array
A = np.array([False, True, False, False, False, True, False, False, False, False, False, True, False])
I can create an "empty" array and then find the indices, propagate them, and then flatten argwhere and then flatten it:
B = np.zeros(A.shape, dtype=bool)
# Compute the indeces of the True values and make the two next to it True as well
idcs_true = np.argwhere(A) + np.arange(propagation + 1)
idcs_true = idcs_true.flatten()
idcs_true = idcs_true[idcs_true < A.size] # in case the error propagation gives a great
B[idcs_true] = True
# Array
print(f'Original array A = {A}')
print(f'New array (2 true) B = {B}')
which gives:
Original array A = [False True False False False True False False False False False True
False]
New array (2 true) B = [False True True True False True True True False False False True
True]
However, this becomes much more complex and fails if for example:
AA = np.array([[False, True, False, False, False, True, False, False, False, False, False, True, False],
[False, True, False, False, False, True, False, False, False, False, False, True, False]])
Thanks for any advice.
