searchsorted#
- named_arrays.searchsorted(a, v, axis=None, side='left', sorter=None)#
Thin wrapper around
numpy.searchsorted().Find the index along
axiswhere each element ofvwould have to be inserted to leaveasorted.This function adds an
axisargument, which letsacarry axes besides the one it is sorted along, wherenumpy.searchsorted()takes a one-dimensionalaonly. Those other axes are batch axes, and each is searched independently of the rest.- Parameters:
a (AbstractArray) – The sorted array to search. It must be increasing along
axis, which is not checked, exactly asnumpy.searchsorted()does not check it.v (float | Quantity | AbstractArray) – The values to find the insertion points of.
axis (None | str) – The logical axis along which
ais sorted. IfNone,amust have exactly one axis.side (Literal['left', 'right']) – Which end of a run of equal values to return the index of.
"left"gives the first such index and"right"the one past the last.sorter (None | AbstractArray) – The indices along
axiswhich sorta, asnumpy.argsort()returns them. IfNone,ais taken to be sorted already.
- Returns:
The indices into
axis, carrying the axes ofvbroadcastagainst the batch axes of
a.
- Return type:
See also
numpy.searchsorted()The function this one wraps.
digitize()Which bin each value falls into, on the same machinery.
Examples
Find where two spectral lines fall in a wavelength grid.
import numpy as np import astropy.units as u import named_arrays as na grid = na.linspace(100, 200, axis="wavelength", num=6) * u.AA line = na.ScalarArray( ndarray=np.array([117, 185]) * u.AA, axes=("line",), ) na.searchsorted(grid, line, axis="wavelength")
ScalarArray( ndarray=[1, 5], shape={'line': 2}, )