2

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?

  • I don't see any loop in your code. Also, please take the [tour](https://stackoverflow.com/tour) and learn [How to Ask](https://stackoverflow.com/help/how-to-ask). In order to get help, you will need to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). If your question include a pandas dataframe, please provide a [reproducible pandas example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – alec_djinn Oct 14 '22 at 09: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 – Francesco Battisti Oct 14 '22 at 09: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? – Yulia V Oct 14 '22 at 09:34
  • Can you explain the logic behind the output? Why start with `2, 3, 4` and not `3, 5, 6`? – T C Molenaar Oct 14 '22 at 09:35
  • Bacause 3, 5, 6 is in array2 and array2_cond is True at these points @TCMolenaar – Francesco Battisti Oct 14 '22 at 09:46
  • Yes, is what I'm looking for @YuliaV – Francesco Battisti Oct 14 '22 at 09:47
  • @MichaelSzczesny your solution works only one time, if array2_cond occurs again, np.maximum.accumulate wont reset and the condition is always True, anyway MY FAULT...(updated) – Francesco Battisti Oct 14 '22 at 10:13

1 Answers1

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.

user7138814
  • 1,991
  • 9
  • 11