views:

94

answers:

2

Hi, i have an numpy array which represents multiple x-intervals of a function:

In [137]: x_foo
Out[137]: 
array([211, 212, 213, 214, 215, 216, 217, 218, 940, 941, 942, 943, 944,
       945, 946, 947, 948, 949, 950])

as you can see, in x_foo are two intervals: one from 211 to 218, and one from 940 to 950. these are intervals, which i want to interpolate with scipy. for this, i need to adjust the spacing, e.g "211.0 211.1 211.2 ..." which you would normaly do with:

arange( x_foo[0], x_foo[-1], 0.1 )

in the case of multiple intervals, this is not possible. so heres my question: is there a numpy-thonic way to do this in array-style? or do i need to write a function which loops over the whole array and split if the difference is >1?

thanks!

+3  A: 
import numpy as np
x = np.array([211, 212, 213, 214, 215, 216, 217, 218, 940, 941, 942, 943, 944,
   945, 946, 947, 948, 949, 950])
ind = np.where((x[1:] - x[:-1]) > 1)[0]

will give you the index for the element in x that is equal to 218. Then the two ranges you want are:

np.arange(x[0],x[ind],0.1)

and

np.arange(x[ind+1],x[-1],0.1)
Justin Peel
thanks for your answer, but thats not exactly what im looking for. i thought there might be a way around this solution. i am using an equal approach right now. thanks anyway!
Heiko Westermann
Then I guess I didn't understand what sort of an answer you were looking for. Can you elaborate a little on what sort of answer you are looking for?
Justin Peel
A: 
np.r_[ 211:218+1, 940:950+1 ]
array([211, 212, 213, 214, 215, 216, 217, 218, 940, 941, 942, 943, 944, 945, 946, 947, 948, 949, 950])

r_[] makes a row out of scalars, ranges, arrays, lists, tuples ...; I guess r_ is short for row. For doc, see np.r_? in Ipython.
(Python handles 211:218 inside square brackets but not round, hence r_[] not () ).

Denis