[Numpy-discussion] indexing with booleans without making a copy?
Wed Sep 8 14:36:31 CDT 2010
2010/9/8 Ernest Adrogué <firstname.lastname@example.org>:
> I have a sorted, flat array:
> In : a =np.array([0,1,2,2,2,3])
> Basically, I want views of the areas where there
> are repeated numbers (since the array is sorted, they
> will be contiguous).
> But, of course, to find the numbers that are repeated
> I have to use comparison operations that return
> boolean arrays, so I suppose the problem is converting
> the boolean array into a slice.
> This is what I have come up with:
> In : np.flatnonzero(a==2)
> Out: array([2, 3, 4])
> In : b=np.flatnonzero(a==2)
> In : b.min()
> Out: 2
> In : b.max()
> Out: 4
> In : a[b.min():b.max()+1]
> Out: array([2, 2, 2])
> If you know a more straightforward way of doing this,
> I'll be glad to know...
add boundaries index zero and end
make two arrays one with starting points, one with endpoints.
> Cheers :)
> NumPy-Discussion mailing list
More information about the NumPy-Discussion