朱莉娅:数组是否包含特定的子数组 [英] Julia: does an Array contain a specific sub-array
问题描述
在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屋!