diff --git a/base/multidimensional.jl b/base/multidimensional.jl index 3c353fa649cd7..854034cd9a566 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -211,6 +211,28 @@ module IteratorsMD CartesianIndex(1, 2) ``` + ## Broadcasting + + `CartesianIndices` support broadcasting arithmetic (+ and -) with a `CartesianIndex`. + + !!! compat "Julia 1.1" + Broadcasting of CartesianIndices requires at least Julia 1.1. + + ```jldoctest + julia> CIs = CartesianIndices((2:3, 5:6)) + 2×2 CartesianIndices{2,Tuple{UnitRange{Int64},UnitRange{Int64}}}: + CartesianIndex(2, 5) CartesianIndex(2, 6) + CartesianIndex(3, 5) CartesianIndex(3, 6) + + julia> CI = CartesianIndex(3, 4) + CartesianIndex(3, 4) + + julia> CIs .+ CI + 2×2 CartesianIndices{2,Tuple{UnitRange{Int64},UnitRange{Int64}}}: + CartesianIndex(5, 9) CartesianIndex(5, 10) + CartesianIndex(6, 9) CartesianIndex(6, 10) + ``` + For cartesian to linear index conversion, see [`LinearIndices`](@ref). """ struct CartesianIndices{N,R<:NTuple{N,AbstractUnitRange{Int}}} <: AbstractArray{CartesianIndex{N},N}