emax February 2016

How to find two consecutive positive-negative values in an array?

I have the following array:

X
array([ 3.5, -3, 5.4, 3.7, 14.9, -7.8, -3.5, 2.1])

For each values of X I know its recording time T. I want to find the indexes between two consecutive positive-negative or viceversa. Concluding I would like an array like

Y = array([ T(1)-T(0), T(2)-T(1),  T(5)-T(4), T(7)-T(6)])

Answers


Robᵩ February 2016

Perhaps iterating over the array in a list comprehension would work for you:

In [35]: x=np.array([ 3.5, -3, 5.4, 3.7, 14.9, -7.8, -3.5, 2.1])

In [36]: y=np.array([b-a for a,b in zip(x, x[1:]) if (a<0) != (b<0)])

In [37]: y
Out[37]: array([ -6.5,   8.4, -22.7,   5.6])

Edit

I apparently didn't understand the question completely. Try this instead:

In [38]: X=np.array([ 3.5, -3, 5.4, 3.7, 14.9, -7.8, -3.5, 2.1])

In [39]: T=np.array([ 0, 0.1, 2, 3.5, 5, 22, 25, 50])

In [40]: y=np.array([t1-t0 for x0,x1,t0,t1 in zip(X, X[1:], T, T[1:]) if (x0<0) != (x1<0)])

In [41]: y
Out[41]: array([  0.1,   1.9,  17. ,  25. ])

Post Status

Asked in February 2016
Viewed 2,413 times
Voted 12
Answered 1 times

Search




Leave an answer