1

I need to slice an array's index from where a first condition is true to where a second condition is true, these conditions are never true at the same time, but one can be true more than one time before the other occurs.
I try to explain:

array_filter = np.array([3,4,5,6,4,3,2,3,4,5])
array1 = np.array([2,3,4,6,3,3,1,2,3,4])
array2 = np.array([3,5,6,7,5,4,3,3,5,6])

array1_cond = array1 >= array_filter
array2_cond = array2 <= array_filter


                0  1  2   3  4  5  6   7  8  9
array_filter    3  4  5   6  4  3  2   3  4  5 
array1          2  3  4   6  3  3  1   2  3  4
array1_cond               ^     ^               (^ = True)
array2          3  5  6   7  5  4  3   3  5  6    
array2_cond     ^                      ^ 

expected_output 2  3  4 | 7  5  4  3 | 2  3  4
                 array1 |   array2   | array1

EXPECTED OUTPUT:

expected_output[(array2_cond) : (array1_cond)] = array1[(array2_cond) : (array1_cond)]
expected_output[(array1_cond) : (array2_cond)] = array2[(array1_cond) : (array2_cond)]
expected_output = [ 2, 3, 4, 7, 5, 4, 3, 2, 3, 4 ]
       

I'm so sorry if syntax is a little confusing, but idk how to make it better... <3
How can I perform this?
Is it possible WITHOUT LOOPS?

7
  • I don't see any loop in your code. Also, please take the tour and learn How to Ask. In order to get help, you will need to provide a minimal reproducible example. If your question include a pandas dataframe, please provide a reproducible pandas example Commented Oct 14, 2022 at 9:28
  • I'm sorry, but this is the best minimal reproducible example I can provide, if I knew how to make it, I wouldn't need help... @alec_djinn Commented Oct 14, 2022 at 9:32
  • Are you 1. looking for an index of the first element that satisfies a condition - let's call it j 2. Concatenate parts of two arrays, array_1[:j] + array_2[j:] - is this what you are doing? Commented Oct 14, 2022 at 9:34
  • Can you explain the logic behind the output? Why start with 2, 3, 4 and not 3, 5, 6? Commented Oct 14, 2022 at 9:35
  • Bacause 3, 5, 6 is in array2 and array2_cond is True at these points @TCMolenaar Commented Oct 14, 2022 at 9:46

1 Answer 1

1

This works for your example, with a, b in place of array1, array2:

nz = np.flatnonzero(a_cond | b_cond)
lengths = np.diff(nz, append=len(a))
cond = np.repeat(b_cond[nz], lengths)
result = np.where(cond, a, b)

If at the start of the arrays neither condition holds true then elements from b are selected.

Sign up to request clarification or add additional context in comments.

3 Comments

Honestly I don't understand but ok, works perfectly and at high speed...thank you so much <3 @user7138814
@Francesco I wish I knew a more straightforward solution with numpy. Personally I think in this case a simple loop accelerated with numba jit would be the best.
>1ms calculations for an array of 5000 floats (with this method) is more than satisfating my friend :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.