[SciPy-user] extracting a range from within an array

Samuel GARCIA sgarcia at olfac.univ-lyon1.fr
Tue May 16 10:06:41 EDT 2006


foo = arange(100)

bar = foo[logical_and(20<foo,foo<40)]


Chris Fonnesbeck a écrit :

>I am trying to extract a section within an array, based on the values
>of the entries. It is easy to use compress() if the subsection is at
>one end or the other:
>
>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)
>
>ValueError: The truth value of an array with more than one element is
>ambiguous. Use a.any() or a.all()
>
>What is the correct way of going about this. Obviously, for this
>simple case, I could simply call arange(20,40), but in an array where
>the contents are unknown a priori, this does not work.
>
>Thanks,
>Chris
>
>  
>




More information about the SciPy-User mailing list