# Index of element with lowest non-negative value

**URL:** <https://discourse.julialang.org/t/index-of-element-with-lowest-non-negative-value/50175>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [November 15, 2020, 7:36am UTC](https://discourse.julialang.org/t/index-of-element-with-lowest-non-negative-value/50175 "2020-11-15T07:36:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)\
**Post date:** [November 15, 2020, 7:36am UTC](https://discourse.julialang.org/t/index-of-element-with-lowest-non-negative-value/50175/1 "2020-11-15T07:36:55Z")

</div>

I need to be able to find the index of the element within a 1d array that has the lowest non-negative value (so excluding all negative numbers). I know one way I finding it, namely using:

```julia
minVal = Inf;
index = 0;
for i in 1:length(arr)
    if arr[i] >= 0 && arr[i] < minVal
        minVal = arr[i];
        index = i;
    end
end

```

I wouldn’t suppose there’s some simpler, higher-performance way of doing this?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 15, 2020, 8:14am UTC](https://discourse.julialang.org/t/index-of-element-with-lowest-non-negative-value/50175/2 "2020-11-15T08:14:49Z")

</div>

You can always add @inbounds to the for loop for an easy gain

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [November 15, 2020, 8:26am UTC](https://discourse.julialang.org/t/index-of-element-with-lowest-non-negative-value/50175/3 "2020-11-15T08:26:44Z")

</div>

Unfortunately there is no method of `argmin` / `argmax` with a filter function as first element.  
Maybe it would be good to add it?  
Otherwise, I also could not see a “nicer” version as a loop. Performance-wise a loop is perfectly fine.

---

<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:** [November 16, 2020, 7:45am UTC](https://discourse.julialang.org/t/index-of-element-with-lowest-non-negative-value/50175/4 "2020-11-16T07:45:46Z")

</div>

Allowing custom comparison operators in `argmin` (`findmin`) would allow a simple solution, since it could just use a comparison operator that always prefers non-negative values. This is WIP that seems stalled at the moment, cf

[https://github.com/JuliaLang/julia/pull/35316](https://github.com/JuliaLang/julia/pull/35316)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 16, 2020, 9:16am UTC](https://discourse.julialang.org/t/index-of-element-with-lowest-non-negative-value/50175/5 "2020-11-16T09:16:07Z")

</div>

> [@fusion809](#):
>
> `if arr[i] >= 0 && arr[i] < minVal`

You can chain comparison operators like this:

```julia
if 0 <= arr[i] < minVal

```
