I have two (sorted) arrays, A and B, of different lengths each containing unique labels that are repeated a number of times. The count for each label in A is less than or equal to that in B. All labels in A will be in B, but some labels in B do not appear in A.
I need an object the same length as B where, for each label i in A (which occurs k_i times), the first k_i occurrences of label i in B need to be set to False.
The remaining elements should be True.
The following code gives me what I need, but if A and B are large, this can take a long time:
import numpy as np
# The labels and their frequency
A = np.array((1,1,2,2,3,4,4,4))
B = np.array((1,1,1,1,1,2,2,3,3,4,4,4,4,4,5,5))
A_uniq, A_count = np.unique(A, return_counts = True)
new_ind = np.ones(B.shape, dtype = bool)
for i in range(len(A_uniq)):
new_ind[np.where(B == A_uniq[i])[0][:A_count[i]]] = False
print(new_ind)
#[False False True True True False False False True False False False
# True True True True]
Is there a faster or more efficient way to do this? I feel like I may be missing some obvious broadcasting or vectorized solution.