朱莉娅:数组是否包含特定的子数组 [英] Julia: does an Array contain a specific sub-array

查看:99
本文介绍了朱莉娅:数组是否包含特定的子数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在julia中,我们可以检查数组是否包含值,如下所示:

In julia we can check if an array contains a value, like so:

> 6 in [4,6,5]
true

但是,当尝试以特定顺序检查子数组时,此方法返回false:

However this returns false, when attempting to check for a sub-array in a specific order:

> [4,6] in [4,6,5]
false

验证数组中是否存在特定子数组的正确语法是什么?

What is the correct syntax to verify if a specific sub-array exists in an array?

推荐答案

对于第三个条件,即向量[4,6]作为4,6,5的子向量出现,建议使用以下功能:

For the third condition i.e. vector [4,6] appears as a sub-vector of 4,6,5 the following function is suggested:

issubvec(v,big) = 
  any([v == slice(big,i:(i+length(v)-1)) for i=1:(length(big)-length(v)+1)])

对于第二种情况,即为set向量中出现的els向量中的每个元素提供一个布尔值,建议如下:

For the second condition, that is, give a boolean for each element in els vectors which appears in set vector, the following is suggested:

function vecin(els,set)
  res = zeros(Bool,size(els))
  res[findin(els,set)]=true
  res
end

将矢量放入OP中,结果为:

With the vector in the OP, these result in:

julia> vecin([4,6],[4,6,5])
2-element Array{Bool,1}:
 true
 true

julia> issubvec([4,6],[4,6,5])
true

这篇关于朱莉娅:数组是否包含特定的子数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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