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


i have following array:

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

for each values of x know recording time t. want find indexes between 2 consecutive positive-negative or viceversa. concluding array like

y = array([ t(1)-t(0), t(2)-t(1),  t(5)-t(4), t(7)-t(6)]) 

perhaps iterating on array in list comprehension work 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 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 question completely. try 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 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. ]) 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -