# Array indexing with multiple conditions

**URL:** <https://discourse.julialang.org/t/array-indexing-with-multiple-conditions/57872>\
**Category:** New to Julia\
**Created:** [March 24, 2021, 2:38pm UTC](https://discourse.julialang.org/t/array-indexing-with-multiple-conditions/57872 "2021-03-24T14:38:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb\_gk](https://avatars.discourse-cdn.com/v4/letter/n/e19b73/32.png) [@newb\_gk](https://discourse.julialang.org/u/newb_gk)\
**Post date:** [March 24, 2021, 2:38pm UTC](https://discourse.julialang.org/t/array-indexing-with-multiple-conditions/57872/1 "2021-03-24T14:38:40Z")

</div>

I have a two-dimensional array and I want to select based on the values in certain columns. For example, let’s think of data to be an 1000x14 Array{Float64,2}. I can do

```julia
data[data[:,3].>-9.0,:]

```

but when I do

```julia
data[data[:,3].>-9.0 & data[:,3].<25.0,:]

```

I get an error that says

```julia
**ERROR:** MethodError: no method matching &(::Float64, ::Array{Float64,1})
Closest candidates are:
&(::Any, ::Any, ::Any, ::Any...) at operators.jl:538
Stacktrace:
[1] top-level scope at **REPL[96]:1**

```

Any suggestions on how I can manage to do this without making use of DataFrames?

Thanks!

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 24, 2021, 2:43pm UTC](https://discourse.julialang.org/t/array-indexing-with-multiple-conditions/57872/2 "2021-03-24T14:43:28Z")

</div>

> [@newb\_gk](#):
>
> ```julia
> data[data[:,3].>-9.0 & data[:,3].<25.0,:]
> 
> ```

There are two things here:

- You need to `.&` just like the other operators to make it work element-wise, too
- You need to use parentheses because `&`'s precedence is different from `&&`.

```julia
data[(data[:,3] .> -9.0) .& (data[:,3] .< 25.0),:]

```

Alternatively, you could combine them into a single chained comparison in this particular case:

```julia
data[-9.0 .< data[:,3] .< 25.0,:]

```

---

<div class="post-metadata">

**Author:** ![newb\_gk](https://avatars.discourse-cdn.com/v4/letter/n/e19b73/32.png) [@newb\_gk](https://discourse.julialang.org/u/newb_gk)\
**Post date:** [March 24, 2021, 5:58pm UTC](https://discourse.julialang.org/t/array-indexing-with-multiple-conditions/57872/3 "2021-03-24T17:58:53Z")

</div>

This is great. Thanks so much for the help!

---

<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:** [March 24, 2021, 8:00pm UTC](https://discourse.julialang.org/t/array-indexing-with-multiple-conditions/57872/4 "2021-03-24T20:00:13Z")

</div>

And since this can a bit visually dense, you can drop some characters without sacrificing performance or clarity:

```julia
data[-9 .< data[:,3] .< 25, :]

```

You don’t need the comparison values to be actual floats.

For performance reasons you could also consider using views.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 24, 2021, 8:29pm UTC](https://discourse.julialang.org/t/array-indexing-with-multiple-conditions/57872/5 "2021-03-24T20:29:51Z")

</div>

> [@DNF](#):
>
> For performance reasons you could also consider using views.

For what it’s worth, I’d expect views to slightly speed up the `data[:,3]` operation and the generation of the logical index but be slower for the final logical indexing expression. YMMV depending upon array types and sizes and downstream uses.
