# Boolean indexing does not work

**URL:** <https://discourse.julialang.org/t/boolean-indexing-does-not-work/15692>\
**Category:** General Usage\
**Created:** [September 30, 2018, 9:18am UTC](https://discourse.julialang.org/t/boolean-indexing-does-not-work/15692 "2018-09-30T09:18:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [September 30, 2018, 9:18am UTC](https://discourse.julialang.org/t/boolean-indexing-does-not-work/15692/1 "2018-09-30T09:18:25Z")

</div>

Reading the documentation I see that boolean indexing is supposed to be the same as indexing with `findall(...)`. However, it doesn’t look like this is correct:

```julia
A = randn(10, 20, 30)
mask = dropdims(sum(A, dims=3), dims=3) .> 0
A[findall(mask), :] # gives the expected result
A[mask, :] # raises BoundsError: attempt to access 10×20×30 Array{Float64,3} at index [Base.LogicalIndex(Bool[...]), Base.Slice(Base.OneTo(30))]

```

Do I overlook the correct way to do this?

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [September 30, 2018, 10:01am UTC](https://discourse.julialang.org/t/boolean-indexing-does-not-work/15692/2 "2018-09-30T10:01:59Z")

</div>

This looks like a bug to me, in  
`checkindex(::Type{Bool}, inds::Tuple, I::Base.LogicalIndex) in Base at multidimensional.jl:542` (currently returns `false`, should instead check sizes). But the multi-dimensional boundschecking code is too byzantine for me to fix this right now.

The following difference between `findall` and logical indexing is probably intentional:

```julia
julia> A_=[1,2,3];b=[true,false,false,false]; A_[findall(b)];
julia> A_[b];
ERROR: BoundsError: ...

```

Edit: On the other hand, this might be intentional, cf [https://github.com/JuliaLang/julia/pull/15431#issuecomment-215469927](https://github.com/JuliaLang/julia/pull/15431#issuecomment-215469927).

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [October 4, 2018, 3:42am UTC](https://discourse.julialang.org/t/boolean-indexing-does-not-work/15692/3 "2018-10-04T03:42:52Z")

</div>

From the [indexing docs](https://docs.julialang.org/en/v1/manual/arrays/index.html#man-supported-index-types-1) - logical indexing segment

> Similarly, indexing by a `N` -dimensional boolean array is effectively the same as indexing by the vector of `CartesianIndex{N}` s where its values are `true` . A logical index must be a vector of the same length as the dimension it indexes into (Single dimension only), or it must be the only index provided and match the size and dimensionality of the array it indexes into (Full size)…

We can adjust the array shape though, so this will work

```julia
reshape(A, 200,30)[mask[:], :]
> ~92x30 Array
```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [October 4, 2018, 7:07pm UTC](https://discourse.julialang.org/t/boolean-indexing-does-not-work/15692/4 "2018-10-04T19:07:12Z")

</div>

Indeed, so it’s documented and not a bug. But anyway, very unintuitive, and would be better not to limit boolean indexing to single dimension.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [October 5, 2018, 8:55pm UTC](https://discourse.julialang.org/t/boolean-indexing-does-not-work/15692/5 "2018-10-05T20:55:46Z")

</div>

Yes, this was intentionally disallowed to make space for this meaning — IIRC it had done something slightly different in the past. It can be added as a feature in 1.x.
