# How to select elements in array within range?

**URL:** <https://discourse.julialang.org/t/how-to-select-elements-in-array-within-range/34866>\
**Category:** New to Julia\
**Created:** [February 19, 2020, 5:12pm UTC](https://discourse.julialang.org/t/how-to-select-elements-in-array-within-range/34866 "2020-02-19T17:12:36Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [February 19, 2020, 5:12pm UTC](https://discourse.julialang.org/t/how-to-select-elements-in-array-within-range/34866/1 "2020-02-19T17:12:36Z")

</div>

Hello,

I would like to know if there is a way to select elements in an array that fall between a range and assign them to a tuple.

```julia
test = sort(rand(10))

p_target = test[6]

```

I would like to select the elements of `test` that fall within `0.1` of `p_target` and assign them to `s_target`. How could this be done?

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [February 19, 2020, 5:17pm UTC](https://discourse.julialang.org/t/how-to-select-elements-in-array-within-range/34866/2 "2020-02-19T17:17:12Z")

</div>

Here are a few ways:

```julia
test[abs.(test .- p_target) .< 0.1]

filter(t -> abs(t-p_target) < 0.1, test)
filter(t -> p_target - 0.1 < t < p_target + 0.1, test)

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 19, 2020, 5:21pm UTC](https://discourse.julialang.org/t/how-to-select-elements-in-array-within-range/34866/3 "2020-02-19T17:21:15Z")

</div>

To add to the previous answer, if `x` is a vector then `tuple(x...)` will make it into a tuple.

---

<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:** [February 19, 2020, 5:43pm UTC](https://discourse.julialang.org/t/how-to-select-elements-in-array-within-range/34866/4 "2020-02-19T17:43:39Z")

</div>

> [@tbeason](#):
>
> `tuple(x...)`

Or `(x...,)`

---

<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:** [February 19, 2020, 5:49pm UTC](https://discourse.julialang.org/t/how-to-select-elements-in-array-within-range/34866/5 "2020-02-19T17:49:37Z")

</div>

> [@improbable22](#):
>
> ```julia
> filter(t -> p_target - 0.1 < t < p_target + 0.1, test)
> 
> ```

Also

```julia
[x for x in test if -0.1<(x-p_target)<0.1]

```

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [February 19, 2020, 6:01pm UTC](https://discourse.julialang.org/t/how-to-select-elements-in-array-within-range/34866/7 "2020-02-19T18:01:18Z")

</div>

Indeed. Also, `Tuple(x)` turns a vector into one, I miseed that part of the question. (Although it seems unlikely that a tuple is going to be the best choice here, but we’d have to know more.)

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [February 20, 2020, 9:36am UTC](https://discourse.julialang.org/t/how-to-select-elements-in-array-within-range/34866/8 "2020-02-20T09:36:31Z")

</div>

Thank you for your replies. I should clarify that I would like `s_target` or `x` to _not_ include the value of `p_target`.

For those interested in the reason, I am trying to model by-catch, which I define as `+` and `-` a certain amount from the body size of the primary target (`p_target`). A different fishing ratio will be applied to `s_target`, so I cannot have the body weight of `p_target` included in `s_target`.

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [February 20, 2020, 11:07am UTC](https://discourse.julialang.org/t/how-to-select-elements-in-array-within-range/34866/9 "2020-02-20T11:07:41Z")

</div>

> [@improbable22](#):
>
> test[abs.(test .- p\_target) .\< 0.1]  
> filter(t → abs(t-p\_target) \< 0.1, test)  
> filter(t → p\_target - 0.1 \< t \< p\_target + 0.1, test)

Could you please explain what the function of `-` is in `test .- p_target` or `t-p_target`? To me it looks like every element of `test` is subtracted by `p_target`, and if the result is less than 0.1, it is recorded. Is this correct? Sorry, my brain feels fuzzy at the moment.

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [February 20, 2020, 12:16pm UTC](https://discourse.julialang.org/t/how-to-select-elements-in-array-within-range/34866/10 "2020-02-20T12:16:43Z")

</div>

Sure. If you print `test .- p_target` it has a zero in the 6th place, and small values nearby. If you print `abs.(test .- p_target) .< 0.1` alone, it is mostly 0 (`false`) with some `true` near to the 6th. You could equally well use `0 .< abs.(test .- p_target) .< 0.1` if you want to exclude values precisely equal to `p_target`.

For the ones with `filter`, you can try replacing that with `map`, and will similarly get a vector of true/false, according to what the function `t -> ...` returns.

---

<div class="post-metadata">

**Author:** ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)\
**Post date:** [February 20, 2020, 12:46pm UTC](https://discourse.julialang.org/t/how-to-select-elements-in-array-within-range/34866/11 "2020-02-20T12:46:22Z")

</div>

> [@HelgavonLichtenstein](#):
>
> p\_target = test[6]

Using [IntervalSets](https://github.com/JuliaMath/IntervalSets.jl) the code can be very elegant:

```julia
s_target = filter(x->x ∈ p_target ± 0.1 && x ≠ p_target, test)

```

🙂

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [February 20, 2020, 2:35pm UTC](https://discourse.julialang.org/t/how-to-select-elements-in-array-within-range/34866/12 "2020-02-20T14:35:51Z")

</div>

@improbable22 thank you for the explanation.

Sorry again. I realize I need the index number, like the `5` in `test[5]`, not the value of what `test[5]` is. But I am glad for all the different ways everyone has shown - they will be handy to learn.

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [February 20, 2020, 2:59pm UTC](https://discourse.julialang.org/t/how-to-select-elements-in-array-within-range/34866/13 "2020-02-20T14:59:51Z")

</div>

OK, then you may want `findall`, which takes a function and a collection just like `filter`, but returns the indices not the elements.

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [February 20, 2020, 3:08pm UTC](https://discourse.julialang.org/t/how-to-select-elements-in-array-within-range/34866/14 "2020-02-20T15:08:12Z")

</div>

Thanks!
