Hello,
I have a sorted, flat array:
In [139]: 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 [146]: np.flatnonzero(a==2) Out[146]: array([2, 3, 4])
In [147]: b=np.flatnonzero(a==2)
In [148]: b.min() Out[148]: 2
In [149]: b.max() Out[149]: 4
In [150]: a[b.min():b.max()+1] Out[150]: array([2, 2, 2])
If you know a more straightforward way of doing this, I'll be glad to know...
Cheers :) Ernest
2010/9/8 Ernest Adrogué eadrogue@gmx.net:
I have a sorted, flat array:
In [139]: 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.
Well, you're going to have to do some allocation, but how's this? Use unique1d to get an array of unique values. Then use searchsorted twice, once to find the leftmost end of each hunk, and once to find the rightmost end of each hunk.
Anne
8/09/10 @ 15:35 (-0400), thus spake Anne Archibald:
2010/9/8 Ernest Adrogué eadrogue@gmx.net:
I have a sorted, flat array:
In [139]: 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.
Well, you're going to have to do some allocation, but how's this? Use unique1d to get an array of unique values. Then use searchsorted twice, once to find the leftmost end of each hunk, and once to find the rightmost end of each hunk.
I like it. Thanks :)
Anne _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
2010/9/8 Ernest Adrogué eadrogue@gmx.net:
Hello,
I have a sorted, flat array:
In [139]: 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 [146]: np.flatnonzero(a==2) Out[146]: array([2, 3, 4])
In [147]: b=np.flatnonzero(a==2)
In [148]: b.min() Out[148]: 2
In [149]: b.max() Out[149]: 4
In [150]: a[b.min():b.max()+1] Out[150]: array([2, 2, 2])
If you know a more straightforward way of doing this, I'll be glad to know...
np.nonzero(np.diff(a))
add boundaries index zero and end make two arrays one with starting points, one with endpoints.
Josef
Cheers :) Ernest _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion