-4

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)])
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
emax
  • 6,965
  • 19
  • 74
  • 141
  • There isn't anything between consecutive elements. I have no idea what you're asking for. – John Gordon Feb 08 '16 at 20:14
  • Take them pairwise, then filter XOR on sign. – Peter Wood Feb 08 '16 at 20:15
  • So far I tried `tmp = np.sign(x[0]) l = list() for i in range(1,len(x)): if (np.sign(x[i]) + tmp)==0: tmp = np.sign(x[i]) deltat = T[i]-T[i-1] l.append(deltat / np.timedelta64(1, 's'))` – emax Feb 08 '16 at 20:51

1 Answers1

2

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. ])
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Great: however the time is stored in another array. How can I do that difference of the corresponding time in the other array? – emax Feb 08 '16 at 21:15
  • Ahh, I misunderstood. Correct answer coming right up. – Robᵩ Feb 08 '16 at 21:29