I have the following code which first selects elements of a NumPy array with a logical index mask:
import numpy as np
grid = np.random.rand(4,4)
mask = grid > 0.5
I wish to use a second boolean mask against this one to pick out objects with :
masklength = len(grid[mask])
prob = 0.5
# generates an random array of bools
second_mask = np.random.rand(masklength) < prob
# this fails to act on original object
grid[mask][second_mask] = 100
This is not quite the same problem as listed in this SO question: Numpy array, how to select indices satisfying multiple conditions? - as I am using random number generation, I don't want to have to generate a full mask, only for the elements selected by the first mask.