I have an numpy.array made by zeros and ones, e.g.:
import numpy
a = numpy.array([0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1])
And now I need to get the first and last index of 1 in each sequence of ones. If I use where, I can get indices of each 1 in the array:
ones = numpy.where(a == 1)
# ones = (array([ 3, 4, 5, 6, 9, 10, 14, 15, 16, 17], dtype=int64),)
But I would like to get only boundaries, it means:
# desired:
ones = (array([ 3, 6, 9, 10, 14, 17], dtype=int64),)
Could you please help me, how to achieve this result? Thank you