Returns the k largest values along a dimension, sorted in decreasing order.
Arguments
- operand
(
arrayish)
Operand.- k
(
integer(1))
Number of top elements to return. Must satisfy1 <= k <= shape(operand)[dim].- dim
(
integer(1)|NULL)
Dimension along which to take the topk. IfNULL(default), uses the last dimension.- with_indices
(
logical(1))
IfFALSE(default), returns just the top-kvalues. IfTRUE, returnslist(values = ..., indices = ...)whereindicesis the 1-based position of each top-kvalue alongdim(dtypei32).
Value
arrayish (when with_indices = FALSE) or named list of two
arrays (when with_indices = TRUE). Output shape matches operand with
dim resized to k; values are sorted decreasing along dim.
NaN handling
NaN ranks larger than any finite value (so it appears first in the
top-k output); -NaN ranks smaller. Unlike nv_sort(), the sign
bit is not canonicalized.
See also
prim_top_k() for the underlying primitive, nv_sort().
Examples
x <- nv_array(c(3, 1, 4, 1, 5, 9, 2, 6))
nv_top_k(x, k = 3L)
#> AnvlArray
#> 9
#> 6
#> 5
#> [ CPUf32{3} ]
nv_top_k(x, k = 3L, with_indices = TRUE)
#> $values
#> AnvlArray
#> 9
#> 6
#> 5
#> [ CPUf32{3} ]
#>
#> $indices
#> AnvlArray
#> 6
#> 8
#> 5
#> [ CPUi32{3} ]
#>
m <- nv_matrix(c(3, 1, 5, 2, 4, 0), nrow = 2, byrow = TRUE)
nv_top_k(m, k = 2L, dim = 2L)
#> AnvlArray
#> 5 3
#> 4 2
#> [ CPUf32{2,2} ]