如果bool的矢量都是谬误,如何检查朱莉娅

时间:2017-10-05 11:18:31

标签: julia

如果有一些预先内置的and()函数或者比这个更好的东西,我就会徘徊:

filter = [true,false,true,false]
length([i for i in filter if i]) > 0 # true

filter = [false,false,false]
length([i for i in filter if i]) > 0 # false

3 个答案:

答案 0 :(得分:4)

julia> x = [true,false,true,false]
4-element Array{Bool,1}:
  true
 false
  true
 false

julia> all(x)
false

对不起,你说'所有的愚蠢'。然后:

julia> all(!, x)

julia> any(x)

答案 1 :(得分:1)

这不是您问题的答案,但请注意filter是现有函数,因此您可能不想覆盖它。

julia> a = [true, false, true, false];
julia> filter(!, a)
 2-element Array{Bool,1}:
 false
 false
julia> filter(!!, a)
 2-element Array{Bool,1}:
 true
 true

答案 2 :(得分:0)

sum()实际上是最快的:

x = falses(1_000_000)

julia> @benchmark sum(x)
BenchmarkTools.Trial:
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     2.834 μs (0.00% GC)
  median time:      2.905 μs (0.00% GC)
  mean time:        3.079 μs (0.00% GC)
  maximum time:     12.648 μs (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     9

julia> @benchmark all(!, x)
BenchmarkTools.Trial:
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     546.055 μs (0.00% GC)
  median time:      546.463 μs (0.00% GC)
  mean time:        558.960 μs (0.00% GC)
  maximum time:     1.709 ms (0.00% GC)
  --------------
  samples:          8928
  evals/sample:     1

@benchmark any(x)
BenchmarkTools.Trial:
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     5.728 μs (0.00% GC)
  median time:      5.752 μs (0.00% GC)
  mean time:        6.044 μs (0.00% GC)
  maximum time:     28.300 μs (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     6