# Array comparison

**URL:** <https://discourse.julialang.org/t/array-comparison/49795>\
**Category:** New to Julia\
**Created:** [November 8, 2020, 4:31pm UTC](https://discourse.julialang.org/t/array-comparison/49795 "2020-11-08T16:31:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![goosst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goosst/32/19090_2.png) [@goosst](https://discourse.julialang.org/u/goosst)\
**Post date:** [November 8, 2020, 4:31pm UTC](https://discourse.julialang.org/t/array-comparison/49795/1 "2020-11-08T16:31:52Z")

</div>

This seems like a very newby question, since I’m trying to do something very simple.  
I want to find the array elements between two values. I can make it work with my third attempt in the code, but it feels like there is a much more elegant way … .

```julia
a=collect(1:30)
b=10
c=20

# attempt 1
findall(a.>b && a.<c) #does not work since bit arrays and boolean operations can't be combined

# attempt 2
comp1=convert(Array{Bool},a.>b)
comp2=convert(Array{Bool},a.<c)
findall(comp1 && comp2) # does not work
findall(comp1. && comp2) # does not work

# attempt 3
idx=findall(a.>b)
idx2=findall(a[idx].<c)
a[idx][idx2]

```

thanks!

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [November 8, 2020, 4:41pm UTC](https://discourse.julialang.org/t/array-comparison/49795/2 "2020-11-08T16:41:00Z")

</div>

You can broadcast `&` but not `&&`, but mind the brackets else you get bitwise `&` of the integers. But you can also write two comparisons at once, `b < a < c`:

```julia
julia> a,b,c = 0:2:50, 10, 20; # no need to collect

julia> findall(b .< a .< c) # indices
4-element Array{Int64,1}:
  7
  8
  9
 10

julia> a[@. (a>b) & (a<c)] # or a[ans], values
4-element Array{Int64,1}:
 12
 14
 16
 18

julia> (a .> b .& a .< c) == (a .> (b .& a) .< c) # bitwise on Int
true

```

You could also write `filter(x -> b<x<c, a)` for the values, or `findall(i -> b<a[i]<c, eachindex(a))` for the indices.

---

<div class="post-metadata">

**Author:** ![malacroi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malacroi/32/19745_2.png) [@malacroi](https://discourse.julialang.org/u/malacroi)\
**Post date:** [November 8, 2020, 4:44pm UTC](https://discourse.julialang.org/t/array-comparison/49795/3 "2020-11-08T16:44:42Z")

</div>

There’s probably a better way, but you can elementwise multiply the bit array, so

```julia
findall((a.>b) .* (a.<c))

```

will give you a bit array for all the indices of the elements you’re looking for. I think you actually want

```julia
a[findall((a.>b) .* (a.<c))]

```

to give the elements.

---

<div class="post-metadata">

**Author:** ![goosst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goosst/32/19090_2.png) [@goosst](https://discourse.julialang.org/u/goosst)\
**Post date:** [November 8, 2020, 7:34pm UTC](https://discourse.julialang.org/t/array-comparison/49795/4 "2020-11-08T19:34:18Z")

</div>

great!, still a lot to learn converting to Julia 🙂

thanks

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [November 8, 2020, 7:50pm UTC](https://discourse.julialang.org/t/array-comparison/49795/5 "2020-11-08T19:50:26Z")

</div>

Just a note - the function `findall` returns indices of elements that are `true` when only given an iterable, or indices of element for which a first-argument function returns `true` when given a function and then an iterable. If you want the elements themselves, use the `filter` function, optionally with a `!` if you want to mutate the iterable (`a` in the original post) that is passed. `filter` takes a function and then an iterable. The way I would do this is

```julia
filter(x -> b<x<c, a)

```
