朱莉娅:检查一个向量中的元素是否在另一向量内 [英] Julia: Check if elements from one vector are within another vector

查看:60
本文介绍了朱莉娅:检查一个向量中的元素是否在另一向量内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一个向量中的元素是否包含在另一个向量中. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆