# Split boolean array into array for sets of trues and false

**URL:** <https://discourse.julialang.org/t/split-boolean-array-into-array-for-sets-of-trues-and-false/53609>\
**Category:** New to Julia\
**Created:** [January 19, 2021, 2:35pm UTC](https://discourse.julialang.org/t/split-boolean-array-into-array-for-sets-of-trues-and-false/53609 "2021-01-19T14:35:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [January 19, 2021, 2:35pm UTC](https://discourse.julialang.org/t/split-boolean-array-into-array-for-sets-of-trues-and-false/53609/1 "2021-01-19T14:35:40Z")

</div>

Is there a function that does something like this:

I have a Boolean array, e.g. `input = [true,true,false,false,false,false,true,true,true,false,false,true]`. I want to get a set of arrays for the corresponding regions of identical value, that is:  
`output=[[true,true],[false,false,false,false],[true,true,true],[false,false],[true]]`.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 19, 2021, 2:43pm UTC](https://discourse.julialang.org/t/split-boolean-array-into-array-for-sets-of-trues-and-false/53609/2 "2021-01-19T14:43:38Z")

</div>

It is unlikely that there is a function that does exactly that. But here is one way to do it:

```julia
julia> input = [true,true,false,false,false,false,true,true,true,false,false,true];

julia> new_input = Vector{Bool}[]
       neq = 1
       for i in 2:length(input)
         if input[i] == input[i-1]
           neq += 1
         else
           push!(new_input,[input[i-1] for j in 1:neq])
           neq = 1
         end
       end

julia> new_input
4-element Array{Array{Bool,1},1}:
 [1, 1]
 [0, 0, 0, 0]
 [1, 1, 1]
 [0, 0]

```

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [January 19, 2021, 2:48pm UTC](https://discourse.julialang.org/t/split-boolean-array-into-array-for-sets-of-trues-and-false/53609/3 "2021-01-19T14:48:38Z")

</div>

Not exactly this form, but easy to get from [StasBase.jl rle](https://juliastats.org/StatsBase.jl/v0.18/misc.html#StatsBase.rle).

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [January 19, 2021, 2:56pm UTC](https://discourse.julialang.org/t/split-boolean-array-into-array-for-sets-of-trues-and-false/53609/4 "2021-01-19T14:56:01Z")

</div>

Thanks for the help, this is useful.
