array subset could be improved? -repost ;)

J

Jim O'D

Hi all

I have an array a=array([2,3,-1]).

I want to extract an array with all the elements of a that are less than 0.

Method 1.
new = array([i for i in a if i < 0])

Method 2.
new = a[nonzero(a<0)]

I'm using Numeric arrays but can't seem to find a function that does this.

Am I missing a more obvious way to do it quickly?

Thanks

Jim
 
J

jon

Jim said:
Hi all

I have an array a=array([2,3,-1]).

I want to extract an array with all the elements of a that are less than 0.

Method 1.
new = array([i for i in a if i < 0])

Method 2.
new = a[nonzero(a<0)]

I'm using Numeric arrays but can't seem to find a function that does this.

Am I missing a more obvious way to do it quickly?

Thanks

Jim
 
J

jon

Jim said:
Hi all

I have an array a=array([2,3,-1]).

I want to extract an array with all the elements of a that are less than 0.

Method 1.
new = array([i for i in a if i < 0])

Method 2.
new = a[nonzero(a<0)]

I'm using Numeric arrays but can't seem to find a function that does this.

Am I missing a more obvious way to do it quickly?

Thanks

Jim

a2=Numeric.compress(a<0,a)
 
F

Fernando Perez

Jim said:
Hi all

I have an array a=array([2,3,-1]).

I want to extract an array with all the elements of a that are less than 0.

Numeric is currently changing into the new scipy core. If you are willing to
play with beta code, get it here:

http://numeric.scipy.org

if not, wait a little for an official release.

With the new numeric, you'll be able to do:

negatives = a[a<0]

Cheers,

f
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top