Say I have a boolean array
a2= np.array([False, False, True, False, False, True, True, True, False, False])
I want an array which contains the number of elements of each group of True values
Desired result:
np.array([1, 3])
Current solution:
sums = []
current_sum = 0
prev = False
for boo in a2:
if boo:
current_sum+=1
prev = True
if prev and not boo:
sums.append(current_sum)
current_sum = 0
if not boo:
prev = False
np.array(sums)
May not be the most computationally efficient. Seems like np.cumsum could be used in a creative manner but I am not able to think of a solution.
