# Picking elements of an array

**URL:** <https://discourse.julialang.org/t/picking-elements-of-an-array/90519>\
**Category:** New to Julia\
**Tags:** array\
**Created:** [November 20, 2022, 1:33am UTC](https://discourse.julialang.org/t/picking-elements-of-an-array/90519 "2022-11-20T01:33:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jisutich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jisutich/32/33342_2.png) [@jisutich](https://discourse.julialang.org/u/jisutich)\
**Post date:** [November 20, 2022, 1:33am UTC](https://discourse.julialang.org/t/picking-elements-of-an-array/90519/1 "2022-11-20T01:33:35Z")

</div>

Hi,

Suppose I have an array `a` like the following

```julia
5×2 Matrix{Float64}:
 2.0 1.0
 3.0 0.0
 4.0 1.0
 2.0 0.0
 4.0 1.0

```

The second column can only be 0 or 1. Now I want to pick the rows whose second element is 0 and store the first elements of these rows into a new array. In the example, I should have

```julia
b =[3.0,4.0]

```

How to achieve this goal?

Also, since my second column can only have 0 or 1. So in principle I can use Boolean variables. How to initiate my array such that the first column is 0(float64) and the second column is false (Bool).

Thanks

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [November 20, 2022, 1:40am UTC](https://discourse.julialang.org/t/picking-elements-of-an-array/90519/2 "2022-11-20T01:40:00Z")

</div>

```julia
julia> a = [ 2.0 1.0
        3.0 0.0
        4.0 1.0
        2.0 0.0
        4.0 1.0]

julia> indices = findall(iszero,a[:,2])
2-element Vector{Int64}:
 2
 4

julia> b = a[indices,1]
2-element Vector{Float64}:
 3.0
 2.0

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [November 20, 2022, 2:21am UTC](https://discourse.julialang.org/t/picking-elements-of-an-array/90519/3 "2022-11-20T02:21:17Z")

</div>

> [@jisutich](#):
>
> How to initiate my array such that the first column is 0(float64) and the second column is false (Bool).

To have a table with different types for each column, you can use DataFrames from the DataFrames package:

```julia
julia> using DataFrames 
  # you might need to install package if it isn't yet

julia> df = DataFrame(a1=[2.0, 3.0, 4.0, 2.0, 4.0],
  a2= [true, false, true, false, true])
5×2 DataFrame                                               
 Row │ a1 a2                                          
     │ Float64 Bool      
─────┼────────────────
   1 │ 2.0 true
   2 │ 3.0 false
   3 │ 4.0 true                                       
   4 │ 2.0 false
   5 │ 4.0 true

julia> df.a1[(!).(df.a2)]
2-element Vector{Float64}:
 3.0
 2.0

```

The last line calculates the result (it uses `!` and broadcasting `.` which require paranthesis because of parsing issues).

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [November 20, 2022, 4:29am UTC](https://discourse.julialang.org/t/picking-elements-of-an-array/90519/4 "2022-11-20T04:29:18Z")

</div>

Array comprehensions are pretty good for this purpose.

```julia
julia> M = Float64[2 1; 3 0; 4 1; 2 0; 4 1]
5×2 Matrix{Float64}:
 2.0 1.0
 3.0 0.0
 4.0 1.0
 2.0 0.0
 4.0 1.0

julia> [first(r) for r in eachrow(M) if last(r) == 1]
3-element Vector{Float64}:
 2.0
 4.0
 4.0

julia> [first(r) for r in eachrow(M) if last(r) == 0]
2-element Vector{Float64}:
 3.0
 2.0

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [November 20, 2022, 11:55am UTC](https://discourse.julialang.org/t/picking-elements-of-an-array/90519/5 "2022-11-20T11:55:23Z")

</div>

> [@jisutich](#):
>
> How to initiate my array such that the first column is 0(float64) and the second column is false (Bool).

The simplest and most direct way is to use a vector of tuples: `A = [(2.0, true), (3.0, false), ...)]`.  
If you need convenient access to components of the array, `StructArrays` are a great fit. They have the same array interface, with extra functionality:

```julia
julia> A = [(2.0, true), (3.0, false), (4.0, true)] |> StructArray

julia> A.:1 # == [2.0, 3.0, 4.0]

julia> A.:2 # == [true, false, true]

julia> A[1] # == (2.0, true)

# your b selection:
julia> b = A.:1[.!A.:2]
# or
julia> filter(x -> !x[2], A).:1

```

Everything here is efficient, and continues to works with all array/collection functions.

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [November 21, 2022, 11:32pm UTC](https://discourse.julialang.org/t/picking-elements-of-an-array/90519/6 "2022-11-21T23:32:50Z")

</div>

> [@Dan](#):
>
> The last line calculates the result (it uses `!` and broadcasting `.` which require paranthesis because of parsing issues).

Slightly off topic: I’ve seen this `(!).` idiom a lot, but `.!` works perfectly fine (with the dot in front, as with any operator):

```julia
julia> x = [false, true, false]
3-element Vector{Bool}:
 0
 1
 0

julia> .!x
3-element BitVector:
 1
 0
 1

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [November 22, 2022, 4:52pm UTC](https://discourse.julialang.org/t/picking-elements-of-an-array/90519/7 "2022-11-22T16:52:56Z")

</div>

> [@aplavin](#):
>
> The simplest and most direct way is to use a vector of tuples: `A = [(2.0, true), (3.0, false), ...)]`.

A tuple of arrays is also simple and direct:

```julia
A = (Float64[2, 3, 4, 2, 4], Bool[1, 0, 1, 0, 1])
([2.0, 3.0, 4.0, 2.0, 4.0], Bool[1, 0, 1, 0, 1])

julia> A[1][A[2]]
3-element Vector{Float64}:
 2.0
 4.0
 4.0

julia> A[1][.!A[2]]
2-element Vector{Float64}:
 3.0
 2.0

```

In fact you could also use a `NamedTuple`:

```julia
julia> NT = (floats = Float64[2, 3, 4, 2, 4], bools = Bool[1, 0, 1, 0, 1])
(floats = [2.0, 3.0, 4.0, 2.0, 4.0], bools = Bool[1, 0, 1, 0, 1])

julia> NT.floats[NT.bools]
3-element Vector{Float64}:
 2.0
 4.0
 4.0

julia> NT.floats[.!NT.bools]
2-element Vector{Float64}:
 3.0
 2.0

```

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [November 22, 2022, 5:53pm UTC](https://discourse.julialang.org/t/picking-elements-of-an-array/90519/8 "2022-11-22T17:53:01Z")

</div>

I don’t think the example in the original post has the correct result – if I (and several other posters) understand correctly, the result should be `b=[3.0,2.0]`.

Other people’s notes about arrays of tuples or struct arrays are probably the better solutions. But to answer the original question directly, this looks like a good candidate for logical indexing:

```julia-repl
julia> a = [2.0 1.0; 3.0 0.0; 4.0 1.0; 2.0 0.0; 4.0 1.0]
5×2 Matrix{Float64}:
 2.0 1.0
 3.0 0.0
 4.0 1.0
 2.0 0.0
 4.0 1.0

julia> b = a[@view(a[:, 2]) .== 0, 1] # the @view() is unnecessary but gives slightly better performance
2-element Vector{Float64}:
 3.0
 2.0

```

Some of the other suggested methods (the comprehensions in particular) might be marginally more efficient than this, however.
