A colleague of mine noticed something strange in the way that getindex and setindex! handle logical indexing with BitMatrix. Consider the following example:
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?