fn

searchsorted

Tensor
searchsorted(sorted_seq: Tensor, values: Tensor, right: bool = ...)
source

Find insertion points to maintain order in a sorted sequence.

For each v = values[i] returns the smallest j such that v < sorted_seq[j] (or <= when right=True). Equivalent to per-element binary search.

Parameters

sorted_seqTensor
1-D sorted sequence of boundaries.
valuesTensor
Query values (arbitrary shape).
rightbool= False
Use <= rather than < for the comparison.

Returns

Tensor

int64 tensor of insertion indices with the same shape as values.

Notes

Sister function bucketize reverses the argument order (values first).

Examples

>>> import lucid
>>> seq = lucid.tensor([1.0, 3.0, 5.0])
>>> lucid.searchsorted(seq, lucid.tensor([0.5, 2.0, 4.0, 6.0]))
Tensor([0, 1, 2, 3])