# Create a boolean mask between arrays of different size

**URL:** <https://discourse.julialang.org/t/create-a-boolean-mask-between-arrays-of-different-size/121986>\
**Category:** General Usage\
**Tags:** arrays, boolean\
**Created:** [October 30, 2024, 11:19am UTC](https://discourse.julialang.org/t/create-a-boolean-mask-between-arrays-of-different-size/121986 "2024-10-30T11:19:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [October 30, 2024, 11:19am UTC](https://discourse.julialang.org/t/create-a-boolean-mask-between-arrays-of-different-size/121986/1 "2024-10-30T11:19:55Z")

</div>

Hi,

I would like to do the same thing as in python:

```julia
import numpy as np

a=[1,2,3,4,5,6,7]
b=[2,4,6]

mask=np.isin(a,b)

```

giving

```julia
>>> mask
array([False, True, False, True, False, True, False])

```

In julia I can:

```julia
mask = indexin(a,b)
7-element Vector{Union{Nothing, Int64}}:
  nothing
 1
  nothing
 2
  nothing
 3
  nothing

```

But I guess that there must be a way to get a simpler boolean array .

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [October 30, 2024, 11:47am UTC](https://discourse.julialang.org/t/create-a-boolean-mask-between-arrays-of-different-size/121986/2 "2024-10-30T11:47:22Z")

</div>

One way is

```julia
[x in b for x in a]

```

or shorter with dot broadcasting

```julia
in.(a, Ref(b))

```

---

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [October 30, 2024, 12:52pm UTC](https://discourse.julialang.org/t/create-a-boolean-mask-between-arrays-of-different-size/121986/3 "2024-10-30T12:52:27Z")

</div>

Thanks !!!

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [October 30, 2024, 1:02pm UTC](https://discourse.julialang.org/t/create-a-boolean-mask-between-arrays-of-different-size/121986/4 "2024-10-30T13:02:30Z")

</div>

In your example, `a` and `b` are both sorted. That permits a fast implementation that scans over both.

If that applies to your true problem, then you should consider doing that instead of the one-liner.

---

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [October 30, 2024, 1:44pm UTC](https://discourse.julialang.org/t/create-a-boolean-mask-between-arrays-of-different-size/121986/5 "2024-10-30T13:44:27Z")

</div>

I don’t use sorted array/matrix here.

I tried both solutions, they are very slow compared to python.  
a have around 25 million elements.  
b have 200 000 elements.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 30, 2024, 1:51pm UTC](https://discourse.julialang.org/t/create-a-boolean-mask-between-arrays-of-different-size/121986/6 "2024-10-30T13:51:03Z")

</div>

> [@Ju\_ska](#):
>
> a have around 25 million elements.  
> b have 200 000 elements.

I think the best solutions depends on what are you eventually doing. Can you show some minimal example of what you’re benchmarking exactly in Numpy?

---

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [October 30, 2024, 1:53pm UTC](https://discourse.julialang.org/t/create-a-boolean-mask-between-arrays-of-different-size/121986/7 "2024-10-30T13:53:19Z")

</div>

Oh it’s quite simple, that’s two arrays of floats.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 30, 2024, 1:56pm UTC](https://discourse.julialang.org/t/create-a-boolean-mask-between-arrays-of-different-size/121986/8 "2024-10-30T13:56:33Z")

</div>

```python
In [1]: import numpy as np

In [2]: a = np.random.rand(25*10**6)

In [3]: b = np.random.rand(2*10**5)

In [4]: %timeit np.isin(a,b)
3.88 s ± 119 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

```

like so?

Also, I was asking what do you do after you get the mask – it may not be the best thing to do depends on what your ultimate goal is.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 30, 2024, 2:04pm UTC](https://discourse.julialang.org/t/create-a-boolean-mask-between-arrays-of-different-size/121986/10 "2024-10-30T14:04:36Z")

</div>

or probably they sort `b` internally:

```julia
In [1]: import numpy as np

In [2]: a = np.random.rand(25*10**6)

In [3]: b = np.random.rand(2*10**5)

In [4]: %timeit np.isin(a,b)
3.88 s ± 119 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

```

edit: they sort it:

> <https://github.com/numpy/numpy/blob/2f7fe64b8b6d7591dd208942f1cc74473d5db4cb/numpy/lib/_arraysetops_impl.py#L988-L991>

in Julia you can also just use `insorted()`, notice the time includes the time to sort `b`

```julia
julia> @be insorted.(a, Ref(sort(b))) evals=10
Benchmark: 1 sample with 10 evaluations
        2.566 s (13 allocs: 6.040 MiB, 0.02% gc time)

```

---

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [October 30, 2024, 2:40pm UTC](https://discourse.julialang.org/t/create-a-boolean-mask-between-arrays-of-different-size/121986/11 "2024-10-30T14:40:54Z")

</div>

Ah thanks for the information, it’s indeed way faster.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [October 30, 2024, 4:43pm UTC](https://discourse.julialang.org/t/create-a-boolean-mask-between-arrays-of-different-size/121986/12 "2024-10-30T16:43:34Z")

</div>

An alternative, which is much faster on my machine:

```julia
in.(a, Ref(Set(b)))

```
