Numpy arange at multiple intervals

I have a numpy array that represents multiple x-bins 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 there are two ranges in x_foo, one from 211 to 218 and one from 940 to 950. These are the ranges that I want to interpolate with scipy. for this I need to adjust the spacing like "211.0 211.1 211.2 ..." which you usually do with:

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

      

this is not possible in the case of multiple intervals. so my question is, is there a multiple way to do this in array style? or do I need to write a function that loops around the entire array and breaks if the difference is> 1?

thanks!

+2


a source to share


2 answers


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 of x, which is 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)

      

+3


a source


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_[]

creates a string from scalars, ranges, arrays, lists, tuples ...; I think r_

not suitable for row

. For the doc, see np.r_?

Ipython.
(Python handles 211: 218 inside square brackets, but not round, hence r_[]

not ()

).

0


a source







All Articles