# Use of findall with different conditions at the same time?

**URL:** <https://discourse.julialang.org/t/use-of-findall-with-different-conditions-at-the-same-time/79656>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 18, 2022, 8:59pm UTC](https://discourse.julialang.org/t/use-of-findall-with-different-conditions-at-the-same-time/79656 "2022-04-18T20:59:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alejandro\_Quiaro\_San](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandro_quiaro_san/32/35437_2.png) [@Alejandro\_Quiaro\_San](https://discourse.julialang.org/u/Alejandro_Quiaro_San)\
**Post date:** [April 18, 2022, 8:59pm UTC](https://discourse.julialang.org/t/use-of-findall-with-different-conditions-at-the-same-time/79656/1 "2022-04-18T20:59:18Z")

</div>

Hi guys.

I am trying to identify values on a matrix that are out of certain boundaries and extracting their positions within the matrix. I am using the function findall in the following way:

The input Matrix is “mat” and the output matrix with the indices is indx.

temp1=(findall(x-\>x\>400, mat)); temp2=(findall(x-\>x\<0, mat)); temp3=(findall(isequal(NaN), mat))  
indx=[temp1; temp2; temp3].

Is there a way to include all the conditions in a single findall? I tried:

indx=(findall(x-\>x\>400&&x\<0&&isequal(NaN), mat));  
indx=(findall(x-\>(x\>400)&&(x\<0)&&(isequal(NaN)), mat));  
indx=(findall(x-\>400\<x\<0&&isequal(NaN), mat));

And its not working.

Any thoughts?

Thanks!

---

<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:** [April 18, 2022, 9:01pm UTC](https://discourse.julialang.org/t/use-of-findall-with-different-conditions-at-the-same-time/79656/2 "2022-04-18T21:01:59Z")

</div>

Do you mean to use ‘or’ (`||`) instead of ‘and’ (`&&`) in your conditions?

---

<div class="post-metadata">

**Author:** ![Alejandro\_Quiaro\_San](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandro_quiaro_san/32/35437_2.png) [@Alejandro\_Quiaro\_San](https://discourse.julialang.org/u/Alejandro_Quiaro_San)\
**Post date:** [April 18, 2022, 9:09pm UTC](https://discourse.julialang.org/t/use-of-findall-with-different-conditions-at-the-same-time/79656/3 "2022-04-18T21:09:58Z")

</div>

Thanks for your reply. Actually the three conditions must be met. x\<0, x\>400 ans x=NaN. The probles is that I had to create three temp files (one for each condition) and then merge them together.

I am wondering if there is a way to have all the three conditions using a single findall (so that I don’t have to create the temp files and merge them afterwards).

---

<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:** [April 18, 2022, 9:15pm UTC](https://discourse.julialang.org/t/use-of-findall-with-different-conditions-at-the-same-time/79656/4 "2022-04-18T21:15:23Z")

</div>

> [@Alejandro\_Quiaro\_San](#):
>
> Actually the three conditions must be met. x\<0, x\>400 ans x=NaN

I don’t think you’ll find a number respecting all three conditions at once, but certainly find numbers respecting one of the conditions at a time (so try changing `&&` to `||` in your `findall`).

---

<div class="post-metadata">

**Author:** ![Alejandro\_Quiaro\_San](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandro_quiaro_san/32/35437_2.png) [@Alejandro\_Quiaro\_San](https://discourse.julialang.org/u/Alejandro_Quiaro_San)\
**Post date:** [April 18, 2022, 9:25pm UTC](https://discourse.julialang.org/t/use-of-findall-with-different-conditions-at-the-same-time/79656/6 "2022-04-18T21:25:45Z")

</div>

I am sorry, actually what I meant to use was “or”, not “and”. Changing && by || now allows me to put x\<0 and x\>400 in the same findall (thanks for that), but I cannot include the isequal(NaN). I get the following error:

TypeError: non-boolean (Base.Fix2{typeof(isequal), Float64}) used in boolean context.

This is running well:  
temp1=(findall(x-\>x\>400||x\<0,t2)); temp2=(findall(isequal(NaN), t2));

This gives the above mentioned error:  
temp1=(findall(x-\>x\>400||x\<0||isequal(NaN),t2))

---

<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:** [April 18, 2022, 9:29pm UTC](https://discourse.julialang.org/t/use-of-findall-with-different-conditions-at-the-same-time/79656/7 "2022-04-18T21:29:36Z")

</div>

> [@Alejandro\_Quiaro\_San](#):
>
> isequal(NaN)

Yeah, should have noticed that one. Comparing with `NaN` is difficult (because `NaN` != `NaN`), but there is a special `isnan` available:

```julia
help?> isnan
search: isnan isinteractive issubnormal DimensionMismatch

  isnan(f) -> Bool

  Test whether a number value is a NaN, an indeterminate value which is neither an infinity nor a finite number ("not a number").

  See also: iszero, isone, isinf, ismissing.

```

---

<div class="post-metadata">

**Author:** ![Alejandro\_Quiaro\_San](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandro_quiaro_san/32/35437_2.png) [@Alejandro\_Quiaro\_San](https://discourse.julialang.org/u/Alejandro_Quiaro_San)\
**Post date:** [April 18, 2022, 9:31pm UTC](https://discourse.julialang.org/t/use-of-findall-with-different-conditions-at-the-same-time/79656/8 "2022-04-18T21:31:56Z")

</div>

Thanks for your comments!

---

<div class="post-metadata">

**Author:** ![halleysfifthinc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/halleysfifthinc/32/206280_2.png) [@halleysfifthinc](https://discourse.julialang.org/u/halleysfifthinc)\
**Post date:** [April 19, 2022, 2:21pm UTC](https://discourse.julialang.org/t/use-of-findall-with-different-conditions-at-the-same-time/79656/9 "2022-04-19T14:21:15Z")

</div>

The error is saying that `isequal(NaN)` returns a function, when you are trying to use it as a boolean. `isequal(NaN)` is a [curried](https://en.wikipedia.org/wiki/Currying) function (the `Base.Fix2` type, which is equivalent to `f(y) = isequal(y, NaN)` ) and you are not giving it `x` (in the anonymous function for `findall`) as an argument.

The solution:

```diff
- findall(x->x>400||x<0||isequal(NaN),t2)
+ findall(x->x>400||x<0||isequal(x,NaN),t2)
+ findall(x->x>400||x<0||isnan(x),t2) # alternately

```

I have recently been in a similar situation and thought that defining `&` and `|` operations for Base.Fix1/Fix2 would be very convenient when multiple curried predicate functions are being used (theoretically also for single argument functions, eg `isnan`, but that would not be as simple to implement). Something along the lines of `findall(>(400)|<(0)|isequal(NaN),t2)`.

---

<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:** [April 19, 2022, 2:39pm UTC](https://discourse.julialang.org/t/use-of-findall-with-different-conditions-at-the-same-time/79656/10 "2022-04-19T14:39:07Z")

</div>

You can also use chained comparison:

```julia
findall(x->!(0<=x<=400)||isnan(x), mat) 

```

---

<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:** [April 19, 2022, 2:58pm UTC](https://discourse.julialang.org/t/use-of-findall-with-different-conditions-at-the-same-time/79656/11 "2022-04-19T14:58:47Z")

</div>

`0<=x<=400` will return `false` for `NaN`, so it should suffice to do `findall(x->!(0≤x≤400), mat)`.

---

<div class="post-metadata">

**Author:** ![Alejandro\_Quiaro\_San](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandro_quiaro_san/32/35437_2.png) [@Alejandro\_Quiaro\_San](https://discourse.julialang.org/u/Alejandro_Quiaro_San)\
**Post date:** [April 19, 2022, 3:45pm UTC](https://discourse.julialang.org/t/use-of-findall-with-different-conditions-at-the-same-time/79656/12 "2022-04-19T15:45:01Z")

</div>

Thank you so much! I used the second option and it was exactly what I was looking for!

---

<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:** [April 19, 2022, 4:15pm UTC](https://discourse.julialang.org/t/use-of-findall-with-different-conditions-at-the-same-time/79656/13 "2022-04-19T16:15:38Z")

</div>

Why not

> [@stevengj](#):
>
> `findall(x->!(0≤x≤400), mat)`

?
