# 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:** 12

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [August 28, 2018, 12:24am UTC](https://discourse.julialang.org/t/what-is-julias-maxk-matlab-that-returns-the-indice-of-top-k-largest-values/14100/12 "2018-08-28T00:24:43Z")

</div>

Got a reasonably fast solution here using the [SortingLab.jl](https://github.com/xiaodaigh/SortingLab.jl), see

> [@Any sorting function in Julia that is as fast as Matlab's maxk?](https://discourse.julialang.org/t/any-sorting-function-in-julia-that-is-as-fast-as-matlabs-maxk/14105/2):
>
> How large is your array? My [SortingLab.jl](https://github.com/xiaodaigh/SortingLab.jl) has a fast sortperm (fsortperm) for sorting integers but not floats. It’s not hard to adapt the algorithm for floats as follows. The issue with fsortperm is that it doesn’t know to stop once it has found the top k values but instead returns everything first so it may not be as efficient. # uint\_mapping stolen from SortingAlgorithms.jl uint\_mapping(x::Float64) = (y = reinterpret(Int64, x); reinterpret(UInt64, ifelse(y \< 0, ~y, xor(y, typemin(Int64))))) …

I am not sure how fast it is compared to Matlab. Perhaps @complexfilter can do a test and share the results.

---

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