# Feature request: generalization of findmax/findmin to n values

**URL:** <https://discourse.julialang.org/t/feature-request-generalization-of-findmax-findmin-to-n-values/10286>\
**Category:** Internals & Design\
**Created:** [April 11, 2018, 5:36pm UTC](https://discourse.julialang.org/t/feature-request-generalization-of-findmax-findmin-to-n-values/10286 "2018-04-11T17:36:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidbp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidbp/32/463_2.png) [@davidbp](https://discourse.julialang.org/u/davidbp)\
**Post date:** [April 11, 2018, 5:36pm UTC](https://discourse.julialang.org/t/feature-request-generalization-of-findmax-findmin-to-n-values/10286/1 "2018-04-11T17:36:38Z")

</div>

Given an array `X` I would like to find the `n` maximum (or minimum) values of the array and the positions where they are located. In matlab there is the following function that just does this

> **[Min/Max selection](https://es.mathworks.com/matlabcentral/fileexchange/23576-min-max-selection)**
>
> Search for k smallest or largest elements in the array

We have `findmax` that already does this for the maximum value. Could we have it for n values?  
Something like `findmax(X,5)` returning 5 values and 5 positions would be very nice.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [April 11, 2018, 5:56pm UTC](https://discourse.julialang.org/t/feature-request-generalization-of-findmax-findmin-to-n-values/10286/2 "2018-04-11T17:56:12Z")

</div>

You can get the same functionality in two lines via `selectperm`:

```julia
julia> function findextrema(v, n; rev=false)
           idx = selectperm(v, 1:n; rev=rev)
           return v[idx], idx
       end

julia> v = rand(-10:10, 5)
5-element Array{Int64,1}:
 -7
  7
  1
  4
  2

julia> findextrema(v,3; rev=true)
([7, 4, 2], [2, 4, 5])

```

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [April 11, 2018, 5:57pm UTC](https://discourse.julialang.org/t/feature-request-generalization-of-findmax-findmin-to-n-values/10286/3 "2018-04-11T17:57:49Z")

</div>

`partialsort` in v0.7 may help you. In v0.6.2 this same function is named `select`.

EDIT: or `partialsortperm` to get the positions.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [April 11, 2018, 6:25pm UTC](https://discourse.julialang.org/t/feature-request-generalization-of-findmax-findmin-to-n-values/10286/4 "2018-04-11T18:25:41Z")

</div>

I agree that it would be nice to have a “more obvious” function for this in `Base` such as what @davidbp suggested.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [April 11, 2018, 11:47pm UTC](https://discourse.julialang.org/t/feature-request-generalization-of-findmax-findmin-to-n-values/10286/5 "2018-04-11T23:47:38Z")

</div>

It looks like the method signature to do this with the function `findmax` is not yet taken.

```julia
function findpartialsort(v,k; opts...)
    inds = partialsortperm(v,k;opts...)
    length(inds) == 1 ? (inds,v[inds]) : (inds,v[[inds...]])
end

Base.findmax(v::AbstractArray,k::AbstractUnitRange; opts...) = findpartialsort(v,k; opts..., rev=true)
Base.findmin(v::AbstractArray,k::AbstractUnitRange; opts...) = findpartialsort(v,k; opts...)

```

```julia
julia> a = [40, 69, 14, 98, 83, 33, 20, 18, 53, 23];

julia> println(sort(a))
[14, 18, 20, 23, 33, 40, 53, 69, 83, 98]

julia> findmin(a,3:4)
([7, 10], [20, 23])

julia> findmax(a,3:4)
([2, 9], [69, 53])

```

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [September 17, 2022, 1:26am UTC](https://discourse.julialang.org/t/feature-request-generalization-of-findmax-findmin-to-n-values/10286/6 "2022-09-17T01:26:36Z")

</div>

I would like to note that I also found myself wanting for this functionality. `partialsortperm` works for `AbstractVector` but not for `AbstractDict`
