Argmax, but n biggest elements

I have an array with values, and I want to find the indices of the top n elements. argmax() allows me to find the biggest one, but not the n biggest, is there a way to modify it?

You could use partialsortperm:

julia> A = rand(1000);

julia> idxs = partialsortperm(A, 1:4, rev=true)
4-element view(::Vector{Int64}, 1:4) with eltype Int64:
 626
 639
 363
 974

julia> A[idxs]
4-element Vector{Float64}:
 0.9999074989526835
 0.9994942615581304
 0.9983178375568715
 0.9959592044733934
10 Likes

Thanks :slight_smile: , didn’t know about that function!

here a discussion on the subject

https://discourse.julialang.org/t/the-quasi-best-maxn-function/52813

2 Likes