Julia:数组是否包含特定的子数组

时间:2016-04-01 00:20:03

标签: arrays julia sub-array

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

> 6 in [4,6,5]
true

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

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

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

5 个答案:

答案 0 :(得分:6)

创建一个性能良好的函数需要一些代码,但这比上面的issubvec版本要快得多:

function subset2(x,y)
    lenx = length(x)
    first = x[1]
    if lenx == 1
        return findnext(y, first, 1) != 0
    end
    leny = length(y)
    lim = length(y) - length(x) + 1
    cur = 1
    while (cur = findnext(y, first, cur)) != 0
        cur > lim && break
        beg = cur
        @inbounds for i = 2:lenx
            y[beg += 1] != x[i] && (beg = 0 ; break)
        end
        beg != 0 && return true
        cur += 1
    end
    false
end

注意:如果函数实际返回子数组开头的位置(如果找到),那么它也会更有用,如果不是,则返回0,类似于findfirst / findnext函数。

计时信息(第二个是使用我的subset2函数):

  0.005273 seconds (65.70 k allocations: 4.073 MB)
  0.000086 seconds (4 allocations: 160 bytes)

答案 1 :(得分:3)

对于第三个条件,即向量[4,6]显示为4,6,5的子向量,建议使用以下函数:

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

对于第二个条件,即为els向量中出现的set向量中的每个元素赋予一个布尔值,建议如下:

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

使用OP中的向量,这些结果为:

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

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

答案 2 :(得分:1)

我最近使用它来查找整数数组中的子序列。它不像@ scott的subset2(x,y)那样好或快......但它会返回索引。

function findsequence(arr::Array{Int64}, seq::Array{Int64})
    indices = Int64[]
    i = 1
    n = length(seq)
    if n == 1
        while true
            occurrence = findnext(arr, seq[1], i)
            if occurrence == 0
                break
            else
                push!(indices, occurrence)
                i = occurrence +1
            end
        end
    else
        while true
            occurrence = Base._searchindex(arr, seq, i)
            if occurrence == 0
                break
            else
                push!(indices, occurrence)
                i = occurrence +1
            end
        end
    end
    return indices
end

julia> @time findsequence(rand(1:9, 1000), [2,3])
    0.000036 seconds (29 allocations: 8.766 KB)
    16-element Array{Int64,1}:
   80
  118
  138
  158
  234
  243
  409
  470
  539
  589
  619
  629
  645
  666
  762
  856

答案 3 :(得分:1)

请注意,您现在可以用点对$(function() { var homeLinks = ['i-t', 'o-c', 'c-f', 'i-c', 'c-u']; var currentLink = 0; var hovered = false; var autoNavInterval; $(".home-link").hover(function() { hovered = true; $('.home-' + homeLinks[currentLink]).hide(); $('[data-hover=' + homeLinks[currentLink] + ']').toggleClass('default-underline'); currentLink = homeLinks.indexOf($(this).attr('data-hover')); $('[data-hover=' + homeLinks[currentLink] + ']').toggleClass('default-underline'); $('.home-' + homeLinks[currentLink]).show(); }, function() { hovered = false; }); function start() { setTimeout(() => { autoNavInterval = setInterval(autoNav, 3000); }, 000); } function reset() { clearInterval(autoNavInterval); currentLink = 0; homeLinks.forEach(function(element) { $('[data-hover=' + element + ']').removeClass('default-underline'); $('.home-' + element).hide(); }); $('.home-' + homeLinks[0]).show(); hovered = false; } function autoNav() { if (hovered === false) { $('.home-' + homeLinks[currentLink]).hide(); $('[data-hover=' + homeLinks[currentLink] + ']').toggleClass('default-underline'); currentLink++; if (currentLink >= homeLinks.length) { currentLink = 1; } $('[data-hover=' + homeLinks[currentLink] + ']').toggleClass('default-underline'); $('.home-' + homeLinks[currentLink]).show(); } } start(); $('.wpb_wrapper').on('mouseout touchend touchcancel', function() { reset(); start(); }); });进行矢量化:

.left-fill {
  background: #0000006b;
  height: 100vh;
}

.right-fill {
  background: pink;
  height: 100vh;
}

.vc_col-sm-6 {
  width: 50%;
  float: left;
}

.pivot-nav {
  list-style: none;
  font-family: 'Montserrat';
  text-align: left;
}

.pivot-nav li a {
  font-size: 1.6rem;
  font-weight: 700;
  text-transform: uppercase;
  display: inline-block;
  position: relative;
  color: #fff;
  text-decoration: none;
  line-height: 40px;
}

.pivot-nav li a.default-underline::after,
.pivot-nav li a:hover::after {
  width: 100%;
}

.pivot-nav:hover a.default-underline:not(:hover)::after {
  width: 0;
}

.pivot-nav li a::after {
  bottom: 0;
  content: "";
  display: block;
  height: 4px;
  position: absolute;
  background: #fff;
  transition: width 0.3s ease 0s;
  width: 0;
}

.home-o-c,
.home-c-f,
.home-i-c,
.home-c-u {
  display: none;
}

.our-company {
  clear: both;
  display: block;
  height: 50vh;
}

.cf2 {
  clear: both;
  display: block;
  height: 50vh;
}

并与<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="left-fill text-left wpb_column vc_column_container vc_col-sm-6"> <div class="wpb_wrapper"> <p class="home-i-t">TEXT One</p> <p class="home-o-c">TEXT One</p> <p class="home-c-f">TExt for C f.</p> <p class="home-i-c">Some more text fo i c.</p> <p class="home-c-u">Get in touch </p> </div> </div> <div class="home-fill right-fill text-right wpb_column vc_column_container vc_col-sm-6"> <div class="wpb_wrapper"> <ul class="pivot-nav"> <li class="pivot-nav-item"><a data-hover="o-c" class="home-link" href="#" data-toggle="my-scrollspy-2">O C</a></li> <li class="pivot-nav-item"><a data-hover="c-f" class="home-link" href="#" data-toggle="my-scrollspy-2">C F</a></li> <li class="pivot-nav-item"><a data-hover="i-c" class="home-link" href="#" data-toggle="my-scrollspy-2">I C</a></li> <li class="pivot-nav-item" data-toggle="my-scrollspy-2"><a data-hover="c-u" class="home-link" href="#">C U</a></li> </ul> </div> </div>链接以获得答案:

in

答案 4 :(得分:0)

我认为值得一提的是,在Julia 1.0中,您具有函数issubset

> issubset([4,6], [4,6,5])
true

您还可以使用\subseteq乳胶符号很方便地调用它

> [4,6] ⊆ [4,6,5]
true

对我来说,这看起来非常优化:

> using Random

> x, y = randperm(10^3)[1:10^2], randperm(10^3);

> @btime issubset(x, y);
16.153 μs (12 allocations: 45.96 KiB)