Suppose that I have an array like this:
my_arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
or a 2D array like:
my_arr = np.array([[1, 1, 11], [2, 1, 0], [3, 3, -1], ..., [10, 9, 0]])
And I define an array like
mask_arr = ([1, 1, 0, 0, 1, 0, 1, 1, 0, 1])
What I want to do from the mask array is to obtain a new array which is consisted of rows, wherein the mask_arr of their index, the element is equal to "1".
For example, the result of the first array would be like:
[1, 2, 0, 0, 5, 0, 7, 8, , 10]
I tried
my_arr[my_mask]
But it didn't work. Is there any solution without wanting to write a for loop to do that?
Thank you in advance
question from:
https://stackoverflow.com/questions/65948120/masking-numpy-arrays-to-select-specific-rows-based-on-another-boolean-array