# Strange logical indexing behavior with 3-dimensional Array and BitMatrix

**URL:** <https://discourse.julialang.org/t/strange-logical-indexing-behavior-with-3-dimensional-array-and-bitmatrix/106221>\
**Category:** General Usage\
**Tags:** indexing, array\
**Created:** [November 14, 2023, 2:47pm UTC](https://discourse.julialang.org/t/strange-logical-indexing-behavior-with-3-dimensional-array-and-bitmatrix/106221 "2023-11-14T14:47:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bgroenks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bgroenks/32/21784_2.png) [@bgroenks](https://discourse.julialang.org/u/bgroenks)\
**Post date:** [November 14, 2023, 2:47pm UTC](https://discourse.julialang.org/t/strange-logical-indexing-behavior-with-3-dimensional-array-and-bitmatrix/106221/1 "2023-11-14T14:47:15Z")

</div>

Hi,

A colleague of mine noticed something strange in the way that `getindex` and `setindex!` handle logical indexing with `BitMatrix`. Consider the following example:

```julia
import Random

X = ones(10,3,3) # Array{Float64,3}
mask = randn(Random.MersenneTwister(1234), 3, 3) .> 0.0 # BitMatrix
X[:,mask] .= 0 # should work...?
# ERROR: BoundsError: attempt to access 10×3×3 Array{Float64, 3} at index [1:10, 3×3 BitMatrix]
X[:,mask,:] .= 0 # works as expected... but why?

```

It seems intuitive that, since `mask` is a matrix matching the last two dimensions of `X`, it should automatically align with the last two axes, and thus the syntax which throws the error should be valid.

From a semantics point of view, I don’t see how the working syntax of including a `:` for the last axis makes any sense, since strictly speaking this operation is not being broadcasted over this axis, and the shape of the subarray returned by `getindex` will not necessarily match `X` along this axis.

Is this maybe a bug in the dispatch pattern for `getindex` and `BitMatrix`?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 14, 2023, 2:53pm UTC](https://discourse.julialang.org/t/strange-logical-indexing-behavior-with-3-dimensional-array-and-bitmatrix/106221/2 "2023-11-14T14:53:27Z")

</div>

if possible I think you want to re-arrange the dimensions:

```julia
julia> X = rand(3,3, 10);

julia> X[mask, :] .= 0;

julia> X
3×3×10 Array{Float64, 3}:
[:, :, 1] =
 0.0 0.600695 0.0
 0.70837 0.0 0.68729
 0.209435 0.0 0.0

[:, :, 2] =
 0.0 0.660355 0.0
 0.434648 0.0 0.727985
 0.58141 0.0 0.0

```

---

<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:** [November 14, 2023, 2:57pm UTC](https://discourse.julialang.org/t/strange-logical-indexing-behavior-with-3-dimensional-array-and-bitmatrix/106221/3 "2023-11-14T14:57:06Z")

</div>

I do think this is a bug in the bounds checking implementation.

```julia-repl
julia> checkbounds(X, to_indices(X, (:, mask))...)
ERROR: BoundsError: attempt to access 10×3×3 Array{Float64, 3} at index [1:10, 3×3 BitMatrix]
Stacktrace:
 [1] throw_boundserror(A::Array{Float64, 3}, I::Tuple{Base.Slice{Base.OneTo{Int64}}, Base.LogicalIndex{Int64, BitMatrix}})
   @ Base ./abstractarray.jl:703
 [2] checkbounds(::Array{Float64, 3}, ::Base.Slice{Base.OneTo{Int64}}, ::Base.LogicalIndex{Int64, BitMatrix})
   @ Base ./abstractarray.jl:668
 [3] top-level scope
   @ REPL[7]:1

julia> checkbounds(X, to_indices(X, (:, mask, 1))...)

julia>

```

It looks like we’re missing a method override to handle the trailing-array-of-cartesian-index case.

---

<div class="post-metadata">

**Author:** ![bgroenks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bgroenks/32/21784_2.png) [@bgroenks](https://discourse.julialang.org/u/bgroenks)\
**Post date:** [November 14, 2023, 4:31pm UTC](https://discourse.julialang.org/t/strange-logical-indexing-behavior-with-3-dimensional-array-and-bitmatrix/106221/4 "2023-11-14T16:31:05Z")

</div>

For performance reasons? I think the indexing should behave consistently regardless of the ordering.
