How to Check if Elements of an Array Belong to a List in Julia
Guide 1
julia> x = rand('a':'c', 10)
10-element Vector{Char}:
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
Let’s say we have an array as shown above. In the example, our goal is to select both 'a'
and 'b'
. Logically, one might think to broadcast with inclusion operator $\in$, but the result is as follows.
julia> x .∈ ['a', 'b']
ERROR: DimensionMismatch("arrays could not be broadcast to a common size; got a dimension with lengths 10 and 2")
A DimensionMismatch
error was raised. This error occurred because broadcasting happened simultaneously to both the array x
and the category ['a', 'b']
. To interpret the error message, it’s confusing because the length 10 of x
and the length 2 of ['a', 'b']
came in at the same time.
julia> x .∈ Ref(['a', 'b'])
10-element BitVector:
1
1
1
0
1
0
0
0
1
1
In this case, you can solve the broadcasting problem with the Ref()
function. This allows 'a'
and 'b'
in ['a', 'b']
to be treated as scalars and find only the places with these two characters.
Precautions
julia> y = rand('a':'c', 1, 10)
1×10 Matrix{Char}:
'b' 'a' 'b' 'b' 'a' 'c' 'a' 'b' 'b' 'c'
Consider the case of a $1 \times 10$ matrix as shown above. At first glance, it might seem no different from the case seen in the guide above, but .∈
is used in a completely different way.
julia> y .∈ ['a', 'b']
2×10 BitMatrix:
0 1 0 0 1 0 1 0 0 0
1 0 1 1 0 0 0 1 1 0
As you can see, the first row indicates the position of 'a'
, and the second row indicates the position of 'b'
. This is due to the difference between a vector and a matrix.
julia> y .∈ Ref(['a', 'b'])
1×10 BitMatrix:
1 1 1 1 1 0 1 1 1 0
Consistent results can be obtained when using Ref()
.
Environment
- OS: Windows
- julia: v1.7.0