# Ignoring \`NaN\` with \`findmax()\`

**URL:** <https://discourse.julialang.org/t/ignoring-nan-with-findmax/39891>\
**Category:** General Usage\
**Tags:** nan\
**Created:** [May 21, 2020, 4:58pm UTC](https://discourse.julialang.org/t/ignoring-nan-with-findmax/39891 "2020-05-21T16:58:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)\
**Post date:** [May 21, 2020, 4:58pm UTC](https://discourse.julialang.org/t/ignoring-nan-with-findmax/39891/1 "2020-05-21T16:58:16Z")

</div>

Suppose I have the following array:

`a=[NaN, 1,2,3]`

If I use `findmax(a)`, I obtain `(NaN, 1)`. How can I use `findmax()` ignoring all `NaN` values? I tried ` findmax(filter(!isnan, a))` but it returns `(3.0,3)` when I want `(3.0,4)`.

Thank you!

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [May 21, 2020, 5:35pm UTC](https://discourse.julialang.org/t/ignoring-nan-with-findmax/39891/2 "2020-05-21T17:35:00Z")

</div>

One bandaid fix is to first set them to `-Inf` (or `Inf` if you are doing `findmin`).

`a[isnan.(a)] .= -Inf`

---

<div class="post-metadata">

**Author:** ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)\
**Post date:** [May 21, 2020, 5:37pm UTC](https://discourse.julialang.org/t/ignoring-nan-with-findmax/39891/3 "2020-05-21T17:37:49Z")

</div>

> [@tbeason](#):
>
> a[isnan.(a)] .= -Inf

Thanks! Worked perfectly!!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 22, 2020, 10:56am UTC](https://discourse.julialang.org/t/ignoring-nan-with-findmax/39891/4 "2020-05-22T10:56:26Z")

</div>

I expected

```julia
findmax(Iterators.filter(!isnan, a))

```

to work but it appers to be broken because of

> <https://github.com/JuliaLang/julia/issues/34674>
>
> Tl;Dr:
> Add
> \`\`\`julia
> Base.keys(gen::Base.Generator) = Base.keys(gen.iter)
> \`\`\`…
> to Base.
> 
> \-----
> 
> Partially related to my comment in https://github.com/JuliaLang/julia/issues/34368#issuecomment-582365454 regarding the support of traits on generators.
> Currently there is no way to use find\* on generators. Reason is that there is no keys function:
> \`\`\`julia
> julia\> findmax(abs(x-1) for x in \[1,5,2,9\])
> ERROR: MethodError: no method matching keys(::Base.Generator{Array{Int64,1},var"#97#98"})
> \`\`\`
> But since generators are said to be the lazy variant of \`map\` and \`map\` preserves the indices it would be good to define that missing \`keys\` method.
> 
> That would allow for some very generic \`findmax(iter)\` and \`argmax(iter)\` as arbitrary lazy maps are allowed while maintaining good performance and low memory footprint.
> 
> Edit: adding the missing find\* and arg\* with custom transform functions would be appreciated aswell to allow for the convenient do-block notation.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [May 22, 2020, 10:59am UTC](https://discourse.julialang.org/t/ignoring-nan-with-findmax/39891/5 "2020-05-22T10:59:57Z")

</div>

I would do:

```julia
julia> findmax(a[.!isnan.(a)])
(3.0, 3)

```

---

<div class="post-metadata">

**Author:** ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)\
**Post date:** [May 22, 2020, 3:41pm UTC](https://discourse.julialang.org/t/ignoring-nan-with-findmax/39891/6 "2020-05-22T15:41:31Z")

</div>

Ahhhh gooood! Another good option! Thanks! But I want `(3.0, 4)` and not `(3.0,3)` as I specified 😉

---

<div class="post-metadata">

**Author:** ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)\
**Post date:** [May 22, 2020, 3:43pm UTC](https://discourse.julialang.org/t/ignoring-nan-with-findmax/39891/7 "2020-05-22T15:43:08Z")

</div>

Ah ok… Right, it doesn’t work ☹

---

<div class="post-metadata">

**Author:** ![joshday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshday/32/368_2.png) [@joshday](https://discourse.julialang.org/u/joshday)\
**Post date:** [May 22, 2020, 5:08pm UTC](https://discourse.julialang.org/t/ignoring-nan-with-findmax/39891/8 "2020-05-22T17:08:15Z")

</div>

MappedArrays.jl is good for this too:

```julia
julia> m = mappedarray(x -> isfinite(x) ? x : -Inf, a);

julia> findmax(m)
(3.0, 4)

```

---

<div class="post-metadata">

**Author:** ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)\
**Post date:** [May 22, 2020, 5:17pm UTC](https://discourse.julialang.org/t/ignoring-nan-with-findmax/39891/9 "2020-05-22T17:17:53Z")

</div>

Ok! Didn’t know about this package! 🙂

---

<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:** [May 25, 2020, 12:22am UTC](https://discourse.julialang.org/t/ignoring-nan-with-findmax/39891/10 "2020-05-25T00:22:53Z")

</div>

> [@Tamas\_Papp](#):
>
> to work but it appers to be broken because of

See also [Define pairs(A) = enumerate(A) · Issue #34851 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/34851)
