# Argmax, but n biggest elements

**URL:** <https://discourse.julialang.org/t/argmax-but-n-biggest-elements/74501>\
**Category:** General Usage\
**Tags:** arrays\
**Created:** [January 12, 2022, 5:50pm UTC](https://discourse.julialang.org/t/argmax-but-n-biggest-elements/74501 "2022-01-12T17:50:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [January 12, 2022, 5:50pm UTC](https://discourse.julialang.org/t/argmax-but-n-biggest-elements/74501/1 "2022-01-12T17:50:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 12, 2022, 6:09pm UTC](https://discourse.julialang.org/t/argmax-but-n-biggest-elements/74501/2 "2022-01-12T18:09:04Z")

</div>

You could use [`partialsortperm`](https://docs.julialang.org/en/v1/base/sort/#Base.Sort.partialsortperm):

```julia
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

```

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [January 12, 2022, 6:25pm UTC](https://discourse.julialang.org/t/argmax-but-n-biggest-elements/74501/3 "2022-01-12T18:25:19Z")

</div>

Thanks 🙂 , didn’t know about that function!

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 13, 2022, 10:32am UTC](https://discourse.julialang.org/t/argmax-but-n-biggest-elements/74501/4 "2022-01-13T10:32:27Z")

</div>

here a discussion on the subject

[https://discourse.julialang.org/t/the-quasi-best-maxn-function/52813](https://discourse.julialang.org/t/the-quasi-best-maxn-function/52813)
