Given a threshold alpha and a numpy array a, there are multiple possibilities for finding the first index i such that arr[i] > alpha; see Numpy first occurrence of value greater than existing value:
numpy.searchsorted(a, alpha)+1
numpy.argmax(a > alpha)
In my case, alpha can be either a scalar or an array of arbitrary shape. I'd like to have a function get_lowest that works in both cases:
alpha = 1.12
arr = numpy.array([0.0, 1.1, 1.2, 3.0])
get_lowest(arr, alpha) # 2
alpha = numpy.array(1.12, -0.5, 2.7])
arr = numpy.array([0.0, 1.1, 1.2, 3.0])
get_lowest(arr, alpha) # [2, 0, 3]
Any hints?
arris always 1d and sorted? And you treat the elements ofalphaindependently? What's the relative size ofarrandalpha? A simple iteration onalphamight be the fastest, especially ifsearchsortedis the optimal tool for a single value.alphacan be an array of arbitrary shape, and is typically much larger thanarr.paddingproblem - with variable length lists. @Divakar's style of solution might be useful, stackoverflow.com/questions/43924187/…