Search code examples
pythonarraysnumpyvectorization

numpy first occurence in array for an array of reference values


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?


Solution

  • searchsorted actually does the trick:

    np.searchsorted(a, alpha)
    

    The axis argument to argmax helps out; this

    np.argmax(numpy.add.outer(alpha, -a) < 0, axis=-1)
    

    does the trick. Indeed

    import numpy as np
    
    a = np.array([0.0, 1.1, 1.2, 3.0])
    
    alpha = 1.12
        
    np.argmax(np.add.outer(alpha, -a) < 0, axis=-1)  # 0
    np.searchsorted(a, alpha)  # 0
    
    alpha = np.array([1.12, -0.5, 2.7])
    np.argmax(np.add.outer(alpha, -a) < 0, axis=-1)  # [2 0 3]
    np.searchsorted(a, alpha)  # [2 0 3]