朱莉娅:检查一个向量中的元素是否在另一向量内 [英] Julia: Check if elements from one vector are within another vector
问题描述
我想检查一个向量中的元素是否包含在另一个向量中. R中有运算符%in%
.
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.
推荐答案
有许多内置插件可以执行类似的操作. indexin
为您提供在b
中找到a
元素的索引(如果不存在,则为0,这类似于R的match
). setdiff
为您提供a
中的元素,而这些元素不在b
中.您很可能可以使用它们来做您想做的-在julia中构造用于过滤的临时布尔数组并不像R中那样意识形态,因为它通常会创建额外的不必要分配.
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.
这篇关于朱莉娅:检查一个向量中的元素是否在另一向量内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!