[SciPy-user] extracting a range from within an array
George Nurser
agn at noc.soton.ac.uk
Tue May 16 10:39:46 EDT 2006
On 16 May 2006, at 15:15, Alan G Isaac wrote:
> On Tue, 16 May 2006, Chris Fonnesbeck apparently wrote:
>> foo = arange(100) bar = foo.compress(foo>40)
>> However, you cannot do the same for a chunk in the middle:
>> bar.foo.compress(20<foo<40)
>
> One simple way below.
>
> Cheers,
> Alan Isaac
>
>>>> x = N.arange(100)
>>>> x[(x>20)*(x<40)]
> array([21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
> 36, 37, 38, 39])
>
My favourite, because it seems most logical, is:
x[(x>20)&(x<40)]
array([21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37,
38, 39])
Similarly
x[(x<20)|(x>80)]
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16,
17, 18, 19, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92,
93, 94,
95, 96, 97, 98, 99])
George.
More information about the SciPy-User
mailing list