fn

kthvalue

tuple of (Tensor, Tensor)
kthvalue(input: Tensor, k: int, dim: int = ..., keepdim: bool = ...)
source

Return the k-th smallest value along dim and its index.

Indices are 1-based: k=1 is the minimum, k=size(dim) is the maximum. Equivalent to sort followed by an index along dim but typically faster (O(n) via quickselect).

Parameters

inputTensor
Source tensor.
kint
Rank of the desired order statistic (1-based).
dimint= -1
Reduction axis.
keepdimbool= False
Retain reduced dim with size 1.

Returns

tuple of (Tensor, Tensor)

(values, indices) — values of the k-th order statistic and their positions along dim.

Notes

Ties are broken by the order in which they appear in input. Behaviour is undefined if k is outside [1, size(dim)].

Examples

>>> import lucid
>>> x = lucid.tensor([5.0, 1.0, 4.0, 2.0, 3.0])
>>> lucid.kthvalue(x, k=2, dim=0)
(Tensor(2.), Tensor(3))