Array creation based in increments

Viewed 36

If I have array A

A = np.array([17, 18, 17.8, 18, 17.7, 17.4, 17, 19, 22])

I want to create the array

[17, 18, 17.8, 18, 17.7, -9999, 17, 19, 22]

The rule: if elements in A are in increasing order they are appended, but, if elements decrease two options are present:

  • If the elements come from a previous increase, they are appended except when the accumulated decrease is less than -0.5; that's why 17.8 is appended, as well as 17.7, but 17.4 is not appended due to the fact that the decrease from 18 to 17.7 is -0.3, added to -0.3 (from 17.7 to 17.4) is less than -0.5 (-9999 is appended then);

  • If we are in a decreasing sequence and we don't come from a previous increase, -9999 is appended.

I think the key is to create an array of consecutive differences and add them in groups of 2 until the sum is below -0.5. But this is very sketchy and I am confused.

Another example: for array

B = np.array([16.0, 15.6, 15.2, 14.8, 14.4, 14.0, 18])

the solution would be

[-9999, -9999, -9999, -9999, -9999, 14.0, 18]
0 Answers
Related