Skip to content

Commit

Permalink
Only allow logical indexing with vectors or...
Browse files Browse the repository at this point in the history
a single index that matches the size of the array it indexes into
  • Loading branch information
mbauman committed Apr 28, 2016
1 parent f520c9d commit 6e508b9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function checkbounds(::Type{Bool}, sz::Integer, r::Range)
@_propagate_inbounds_meta
isempty(r) | (checkbounds(Bool, sz, first(r)) & checkbounds(Bool, sz, last(r)))
end
checkbounds(::Type{Bool}, sz::Integer, I::AbstractArray{Bool}) = length(I) == sz
checkbounds{N}(::Type{Bool}, sz::Integer, I::AbstractArray{Bool,N}) = N == 1 && length(I) == sz
function checkbounds(::Type{Bool}, sz::Integer, I::AbstractArray)
@_inline_meta
b = true
Expand Down
2 changes: 1 addition & 1 deletion base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ using .IteratorsMD
# improvement over the general definitions in abstractarray.jl
for N = 1:5
args = [:($(symbol(:I, d))) for d = 1:N]
targs = [:($(symbol(:I, d))::Union{Colon,Number,AbstractVector}) for d = 1:N] # prevent co-opting the CartesianIndex version
targs = [:($(symbol(:I, d))::Union{Colon,Number,AbstractArray}) for d = 1:N] # prevent co-opting the CartesianIndex version
exs = [:(checkbounds(Bool, size(A, $d), $(args[d]))) for d = 1:N]
cbexpr = exs[1]
for d = 2:N
Expand Down
15 changes: 9 additions & 6 deletions doc/manual/arrays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ where each ``I_k`` may be:
2. A ``Range`` of the form ``a:b``, or ``a:b:c``
3. A ``:`` or ``Colon()`` to select entire dimensions
4. An arbitrary integer array, including the empty array ``[]``
5. A boolean array to select elements at its ``true`` indices
5. A boolean array to select a vector of elements at its ``true`` indices

If all the indices are scalars, then the result ``X`` is a single element from
the array ``A``. Otherwise, ``X`` is an array with the same number of
Expand All @@ -282,11 +282,14 @@ indexed with scalars are dropped. For example, the result of ``A[2, I, 3]`` is
an array with size ``size(I)``. Its ``i``\ th element is populated by
``A[2, I[i], 3]``.

Boolean arrays must be the same length as the dimension they are indexing into.
Indexing by a boolean array ``B`` is the same as indexing by the vector that is
returned by ``find(B)``; the size of a dimension indexed by a boolean array will
be the number of true values in the vector. It is generally more efficient to
use boolean arrays as indices directly instead of first calling ``find``.
Indexing by a boolean array ``B`` is effectively the same as indexing by the
vector that is returned by :func:`find(B) <find>`. Often referred to as logical
indexing, this selects elements at the indices where the values are ``true``,
akin to a mask. A logical index must be a vector of the same length as the
dimension it indexes into, or it must be the only index provided and match the
size and dimensionality of the array it indexes into. It is generally more
efficient to use boolean arrays as indices directly instead of first calling
:func:`find`.

Additionally, single elements of a multidimensional array can be indexed as
``x = A[I]``, where ``I`` is a ``CartesianIndex``. It effectively behaves like
Expand Down

0 comments on commit 6e508b9

Please sign in to comment.