# Is there any way to use max() and min() based on a function?

**URL:** <https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350>\
**Category:** General Usage\
**Tags:** syntax\
**Created:** [December 16, 2019, 7:38pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350 "2019-12-16T19:38:19Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![yelircaasi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yelircaasi/32/16143_2.png) [@yelircaasi](https://discourse.julialang.org/u/yelircaasi)\
**Post date:** [December 16, 2019, 7:38pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350/1 "2019-12-16T19:38:20Z")

</div>

This is easily done in, for example, Python, and can be very useful. I have spent quite a bit of time searching documentation and reading forum questions, without finding anything.

Here’s a small example of what I would like to do:

```julia
arr = [1,2,3,4,-5]
sqmax = max(arr, by = x -> x^2)

```

I know this can be done as follows, but it seems like it would make sense for max() to have a `by` option.

```julia
sqmax = sort(arr, by = x -> x^2)[end]

```

---

<div class="post-metadata">

**Author:** ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Post date:** [December 16, 2019, 8:13pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350/2 "2019-12-16T20:13:58Z")

</div>

You are looking for what is called `maximum` in Julia. Take a look at the documentation on `max` and `maximum` for more examples.

Welcome to the forum. A question like this is better suited for the “Usage” category.

---

<div class="post-metadata">

**Author:** ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)\
**Post date:** [December 16, 2019, 8:16pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350/3 "2019-12-16T20:16:00Z")

</div>

But still there is no built in `maxiumum` function offering whats requested.

I’d go with:  
`findmax(map(x->x^2,arr))`

_Update: this is only needed if you want the index, which I initially thought was asked._

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [December 16, 2019, 8:17pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350/4 "2019-12-16T20:17:06Z")

</div>

`maximum` does take an optional function as its first argument. Try `maximum(x -> x^2, arr)`.

---

<div class="post-metadata">

**Author:** ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)\
**Post date:** [December 16, 2019, 8:18pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350/5 "2019-12-16T20:18:53Z")

</div>

why not:

```julia
maximum(arr.^2)

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [December 16, 2019, 8:21pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350/6 "2019-12-16T20:21:43Z")

</div>

The problem with that is that it creates a temporary array, which can be avoided by passing the function as an argument.

---

<div class="post-metadata">

**Author:** ![under-Peter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/under-peter/32/3626_2.png) [@under-Peter](https://discourse.julialang.org/u/under-Peter)\
**Post date:** [December 16, 2019, 8:22pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350/7 "2019-12-16T20:22:08Z")

</div>

That allocates an intermediate array for the squared array whereas the version from simon does not. It would be nice if we could reduce over a broadcasted array without allocs one day but we’re not there yet.

---

<div class="post-metadata">

**Author:** ![Rex\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rex_wang/32/35108_2.png) [@Rex\_Wang](https://discourse.julialang.org/u/Rex_Wang)\
**Post date:** [January 20, 2022, 2:16pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350/8 "2022-01-20T14:16:01Z")

</div>

thanks for the answer, but `maximum(f, arr)` will return the maximal element in `f(arr)` rather than in `arr`.  
Is there any keyword of `maximum` or other functons to “find x in arr s.t. f(x) is maximal”

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [January 20, 2022, 2:23pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350/9 "2022-01-20T14:23:02Z")

</div>

Example:

```julia
xs = [1, 3, 2]; i = argmax([x^2 for x in xs]); println(xs[i])

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 20, 2022, 2:28pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350/10 "2022-01-20T14:28:28Z")

</div>

You can also pass the function to argmax, to avoid the allocation of the intermediate array:

```julia
julia> x = [1,3,2]
3-element Vector{Int64}:
 1
 3
 2

julia> argmax(x -> -x^2, x)
1

julia> argmax(x -> x^2, x)
3

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 20, 2022, 2:29pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350/11 "2022-01-20T14:29:02Z")

</div>

```julia
julia> findmax(abs2, -3:2)
(9, 1)

```

the second returned value can be used to index the `arr`

or just use:

```julia
julia> argmax(abs2, -3:2)
-3

```

which does the `indexing` for you under the hood

---

<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 20, 2022, 2:44pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350/12 "2022-01-20T14:44:52Z")

</div>

```julia
foldl((x,y)->x^2>y^2 ? x : y, arr)

```

or

```julia
foldl((x,y)->ifelse(x^2>y^2 , x , y), arr)

```

```julia
maxby(f,arr)=foldl((x,y)->f(x)>f(y) ? x : y, arr)

```

---

<div class="post-metadata">

**Author:** ![nharr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nharr/32/24698_2.png) [@nharr](https://discourse.julialang.org/u/nharr)\
**Post date:** [January 26, 2022, 5:58pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350/13 "2022-01-26T17:58:02Z")

</div>

> [@jling](#):
>
> or just use:
> 
> ```julia
> julia> argmax(abs2, -3:2)
> -3
> 
> ```
> 
> which does the `indexing` for you under the hood

Yes, for those wondering, this feature was added in Julia v1.7. Quite useful/convenient on occasion.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 26, 2022, 7:48pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350/14 "2022-01-26T19:48:55Z")

</div>

> [@Rex\_Wang](#):
>
> Is there any keyword of `maximum` or other functons to “find x in arr s.t. f(x) is maximal”

Open issue up for grabs: [Add a `by` keyword to `maximum` and `minimum` · Issue #28210 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/28210)

(Of course, you could just write your own loop for this, which is perfectly fast.)

---

<div class="post-metadata">

**Author:** ![Rex\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rex_wang/32/35108_2.png) [@Rex\_Wang](https://discourse.julialang.org/u/Rex_Wang)\
**Post date:** [February 23, 2022, 2:31pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350/15 "2022-02-23T14:31:09Z")

</div>

Thanks! I found out this implementation is new in Julia 1.7. For 1.6 or lower version, it still can be done by a less direct way

```nohighlight
data[argmax(f.data)]

```
