# Findall function

**URL:** <https://discourse.julialang.org/t/findall-function/33484>\
**Category:** New to Julia\
**Created:** [January 17, 2020, 11:27am UTC](https://discourse.julialang.org/t/findall-function/33484 "2020-01-17T11:27:54Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![anirudhpammi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anirudhpammi/32/13819_2.png) [@anirudhpammi](https://discourse.julialang.org/u/anirudhpammi)\
**Post date:** [January 17, 2020, 11:27am UTC](https://discourse.julialang.org/t/findall-function/33484/1 "2020-01-17T11:27:54Z")

</div>

I have a question regarding the internal code of the findall function. I have an array and I want to find elements that satisfy two conditions. I know that I can use something like findall(f:function,A) where A is the array. I have the following code:

A = [1;2;3;4;5];  
findall(x-\>1\<x\<4,A)  
findall(x-\>x\>1&&x\<4,A)  
findall(x-\>x\>1&x\<4,A)  
findall(x-\>x\<4&x\>1,A)

The first two commands return the correct answer: 2,3. The third one returns 2,3,4,5 and the fourth one returns 0-element Array.  
Why don’t the last two commands work?

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [January 17, 2020, 11:45am UTC](https://discourse.julialang.org/t/findall-function/33484/2 "2020-01-17T11:45:23Z")

</div>

This may be due to operator precedence. There is a table about this here  
[https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity-1](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity-1)

Have you tried adding backets?

findall(x-\>(x\<4)&&(x\>1),A)

Also you may want to try logical and, i.e. && instead of & (bitwise and)

---

<div class="post-metadata">

**Author:** ![Jakub\_Wronowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakub_wronowski/32/204030_2.png) [@Jakub\_Wronowski](https://discourse.julialang.org/u/Jakub_Wronowski)\
**Post date:** [January 17, 2020, 12:58pm UTC](https://discourse.julialang.org/t/findall-function/33484/3 "2020-01-17T12:58:11Z")

</div>

You might also like this syntax:

```julia
A[1 .< A .< 4]

```

---

<div class="post-metadata">

**Author:** ![anirudhpammi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anirudhpammi/32/13819_2.png) [@anirudhpammi](https://discourse.julialang.org/u/anirudhpammi)\
**Post date:** [January 17, 2020, 1:00pm UTC](https://discourse.julialang.org/t/findall-function/33484/4 "2020-01-17T13:00:26Z")

</div>

The logical and works with or without the brackets (command 2). I was just curious why the bitwise operation doesn’t work as I would imaging the function generates two bits for each element of the array and then computes the and operation, which should result in the correct answer. I found information on the precedence for && and || but not on bit wise operations.

---

<div class="post-metadata">

**Author:** ![anirudhpammi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anirudhpammi/32/13819_2.png) [@anirudhpammi](https://discourse.julialang.org/u/anirudhpammi)\
**Post date:** [January 17, 2020, 1:04pm UTC](https://discourse.julialang.org/t/findall-function/33484/5 "2020-01-17T13:04:26Z")

</div>

Thank you, this works. But I was wondering why the bitwise operations don’t work in the findall function.

---

<div class="post-metadata">

**Author:** ![Jakub\_Wronowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakub_wronowski/32/204030_2.png) [@Jakub\_Wronowski](https://discourse.julialang.org/u/Jakub_Wronowski)\
**Post date:** [January 17, 2020, 1:12pm UTC](https://discourse.julialang.org/t/findall-function/33484/6 "2020-01-17T13:12:55Z")

</div>

See Multiplication section.

Place to check operator priority for operators is also this file:

> <https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm>

You can check there f. e. that ⅋ unicode symbol has the same priority as & and \*.

---

<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:** [January 17, 2020, 1:15pm UTC](https://discourse.julialang.org/t/findall-function/33484/7 "2020-01-17T13:15:12Z")

</div>

Or just try:

```julia
julia> x=5
5

julia> x>1&x<4
true

julia> (x>1)&(x<4)
false

```

---

<div class="post-metadata">

**Author:** ![anirudhpammi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anirudhpammi/32/13819_2.png) [@anirudhpammi](https://discourse.julialang.org/u/anirudhpammi)\
**Post date:** [January 17, 2020, 3:01pm UTC](https://discourse.julialang.org/t/findall-function/33484/8 "2020-01-17T15:01:28Z")

</div>

I finally understand it now. It evaluates 1&x as a quantity, since x is nonzero, the value is computed as 1, then the conditions read as x\>1\<4 which is true. Thus the result is true, when you put them in the bracket it evaluates it as two conditions and gives the right answer.  
The same logic applied to my initial question. The third condition reads as: x\>1\<4 (since all my entries are nonzero), all values other than the first satisfy the condition, thus the answer. The fourth condition reads as x\<1\>1 which is not true thus it doesn’t return anything.

---

<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:** [January 17, 2020, 3:16pm UTC](https://discourse.julialang.org/t/findall-function/33484/9 "2020-01-17T15:16:47Z")

</div>

> [@anirudhpammi](#):
>
> It evaluates 1&x as a quantity, since x is nonzero, the value is computed as 1

since x is uneven !

e.g.:

```julia
julia> 1&5
1

julia> 1&4
0

```

---

<div class="post-metadata">

**Author:** ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)\
**Post date:** [January 17, 2020, 3:21pm UTC](https://discourse.julialang.org/t/findall-function/33484/10 "2020-01-17T15:21:51Z")

</div>

You can use `dump` on an expressions to see how the syntax is parsed which can be helpful.

```julia
dump(:(x>1&x<4))
Expr
  head: Symbol comparison
  args: Array{Any}((5,))
    1: Symbol x
    2: Symbol >
    3: Expr
      head: Symbol call
      args: Array{Any}((3,))
        1: Symbol &
        2: Int64 1
        3: Symbol x
    4: Symbol <
    5: Int64 4

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [January 18, 2020, 5:40pm UTC](https://discourse.julialang.org/t/findall-function/33484/11 "2020-01-18T17:40:13Z")

</div>

You can also try a generator for efficiency, and you can iterate over or materialize using `collect` if needed.

```julia
(x for x in A if 1 < x < 4)

```
