Given a tuple of ordered 1D-arrays (arr1, arr2, arr3, ), which would be the best way to get a tuple of min/max indices ((min1, max1), (min2, max2), (min3, max3), ) so that the arrays span the largest common range?
What I mean is that
min(arr[min1], arr2[min2], arr3[min3]) > max(arr1[min1-1], arr2[min2-1], arr3[min3-1])
and
max(arr[min1], arr2[min2], arr3[min3]) < min(arr1[min1+1], arr2[min2+1], arr3[min3+1])
the same for the upper bounds?
An example:
Given arange(12) and arange(3, 8), I want to get ((3,8), (0,6)), with the goal that arange(12)[3:8] == arange(3,8)[0:6].
EDIT Note that the arrays can be float or integer.
Sorry if this is confusing; I cannot find easier words right now. Any help is greatly appreciated!
EDIT2 / answer I just realize that I was terrible at formulating my question. I ended up solving what I wanted like this:
mins = [np.min(t) for t in arrays]
maxs = [np.max(t) for t in arrays]
lower_bound = np.max(mins)
upper_bound = np.min(maxs)
lower_row = [np.searchsorted(arr, lower_bound, side='left') for arr in arrays]
upper_row = [np.searchsorted(arr, upper_bound, side='right') for arr in arrays]
result = zip(lower_row, upper_row)
However, both answers seem to be valid for the question I asked, so I’m unsure to select only one of them as ‘correct’ – what should I do?
I’m sure there are different ways to do this, I would use a merge algorithm to walk through the two arrays, keeping track of overlap regions. If you’re not familiar with the idea take a look at merge-sort, hopefully between that and the code it’s clear how this works.
Note that end here is returned in python convention so that if
a=[0, 1, 2]andb=[0, 1, 4],start=(0, 0)andend=(2, 2).