# What is Julia's maxk(Matlab) that returns the indice of top K largest values?

**URL:** <https://discourse.julialang.org/t/what-is-julias-maxk-matlab-that-returns-the-indice-of-top-k-largest-values/14100>\
**Category:** General Usage\
**Tags:** sort, sortperm\
**Created:** [August 26, 2018, 10:48pm UTC](https://discourse.julialang.org/t/what-is-julias-maxk-matlab-that-returns-the-indice-of-top-k-largest-values/14100 "2018-08-26T22:48:09Z")\
**Posts on this page:** 1\
**Showing post:** 15

<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:** [May 9, 2023, 11:05am UTC](https://discourse.julialang.org/t/what-is-julias-maxk-matlab-that-returns-the-indice-of-top-k-largest-values/14100/15 "2023-05-09T11:05:14Z")

</div>

[here a similar discussion](https://discourse.julialang.org/t/the-quasi-best-maxn-function/52813/12)

```julia
julia> using DataStructures

julia> function MaxN7(cr,N)
           maxn = heapify!(cr[1:N])
           maxn1=maxn[1]
              @inbounds for i in N+1:length(cr)
               e=cr[i]
               if maxn1 < e
                   heappop!(maxn)
                   heappush!(maxn,e)
                   maxn1=maxn[1]
                   end
               end
           sort!(maxn,rev=true)
       end
MaxN7 (generic function with 1 method)

# on my laptop

julia> @btime MaxN7(x, 5) setup=(x=randn(1000));
  722.951 ns (1 allocation: 96 bytes)

julia> @btime bottomk(x, 5) setup=(x=randn(1000));
  752.137 ns (5 allocations: 320 bytes)

```

---

_[View the full topic](https://discourse.julialang.org/t/what-is-julias-maxk-matlab-that-returns-the-indice-of-top-k-largest-values/14100)._
