[Numpy-discussion] Indexing in Numpy vs. IDL?

Jason Woolard Jason.Woolard@noaa....
Sun Nov 16 16:15:51 CST 2008


hi all,

I'm fairly new to Numpy and I've been trying to port over some IDL code 
to become more familiar. I've been moderately successful with 
numpy.where and numpy.compress to do some of things that were pretty 
easy to do in IDL. I'm a bit confused about how the indexing of arrays 
works though.

This is pretty straightforward:

in IDL
=============================
data = [50.00, 100.00, 150.00, 200.00, 250.00, 300.00, 350.00]
index = WHERE((data GT 100.00) AND (data LT 300.00))
new_data = data[index]
print, new_data

150.000      200.000      250.000

in Python
==============================
 >>> import numpy
 >>> from numpy import *
 >>> data = [50.00, 100.00, 150.00, 200.00, 250.00, 300.00, 350.00]
 >>> data = array(data, dtype=float32) #Convert list to array
 >>> index_mask = numpy.where((data > 100.00) & (data < 300.00), 1,0) 
#Test for the condition.
 >>> index_one = numpy.compress(index_mask, data)
 >>> print index_one
[ 150.  200.  250.]


But I'm having a bit of trouble with the Python equivalent of this:

in IDL:
=============================
index_two = WHERE ((data[index_one]  GT bottom) AND (data[index_one] LE  
top)

and also this:

result = MAX(data[index_one[index_two]])

 From what I've read it looks like numpy.take() might work to do the 
indexing. I've tried to test this but I'm not getting the answers I'd 
expect. Am I overlooking something obvious here?

Thanks in advance for any responses.



More information about the Numpy-discussion mailing list