问题
I would like to check if the elements in one vector are contained within another vector. In R there is the operator %in%
.
For example the operator would do the following:
[1,3,5,7,9,4] %in% [1,2,4,5,8,9,10,11]
# [true,false,true,false,true,true]
I can easily write my own only I am trying not to reinvent the wheel.
回答1:
There are a number of built-ins that do something similar. indexin
gives you the indices in b
where the elements of a
are found (0 if it is not there - this is similar to R's match
). setdiff
gives you the elements in a
that are not in b
. It is likely you'll be able to do what you want with these - constructing temporary boolean arrays for filtering is not so ideomatic in julia as in R, as it generally creates an extra, unnecessary allocation.
回答2:
Probably not so nice, but you could do:
julia> [1,3,5,7,9,4] .∈ [[1,2,4,5,8,9,10,11]]
6-element BitArray{1}:
true
false
true
false
true
true
回答3:
You could use an anonymous function : map(x -> x in [1,2,4,5,8,9,10,11] ,[1,3,5,7,9,4])
Or a comprehension : [x in [1,2,4,5,8,9,10,11] for x = [1,3,5,7,9,4]]
来源:https://stackoverflow.com/questions/46411683/julia-check-if-elements-from-one-vector-are-within-another-vector