?eachindex
search: eachindex
eachindex(A...)
Create an iterable object for visiting each index of an AbstractArray
A
in an efficient manner. For array types that have opted into fast linear indexing (like Array
), this is simply the range 1:length(A)
. For other array types, return a specialized Cartesian range to efficiently index into the array with indices specified for every dimension. For other iterables, including strings and dictionaries, return an iterator object supporting arbitrary index types (e.g. unevenly spaced or non-integer indices).
If you supply more than one AbstractArray
argument, eachindex
will create an iterable object that is fast for all arguments (a UnitRange
if all inputs have fast linear indexing, a CartesianIndices
otherwise). If the arrays have different sizes and/or dimensionalities, a DimensionMismatch exception will be thrown.
jldoctest
julia> A = [1 2; 3 4];
julia> for i in eachindex(A) # linear indexing
println(i)
end
1
2
3
4
julia> for i in eachindex(view(A, 1:2, 1:1)) # Cartesian indexing
println(i)
end
CartesianIndex(1, 1)
CartesianIndex(2, 1)
A = rand(3, 4)
3×4 Array{Float64,2}: 0.0549109 0.925113 0.792561 0.552081 0.448623 0.183203 0.309245 0.0209454 0.0930247 0.590689 0.0175255 0.512091
for i in eachindex(A)
@show i, A[i]
end
(i, A[i]) = (1, 0.05491091194983966) (i, A[i]) = (2, 0.4486226144552561) (i, A[i]) = (3, 0.09302471246596777) (i, A[i]) = (4, 0.9251127264830541) (i, A[i]) = (5, 0.18320348558132737) (i, A[i]) = (6, 0.5906888424135703) (i, A[i]) = (7, 0.7925610544786179) (i, A[i]) = (8, 0.309244948319527) (i, A[i]) = (9, 0.017525501252093845) (i, A[i]) = (10, 0.5520811765566824) (i, A[i]) = (11, 0.020945370945398833) (i, A[i]) = (12, 0.5120912274896219)
?CartesianIndex
search: CartesianIndex CartesianIndices
CartesianIndex(i, j, k...) -> I
CartesianIndex((i, j, k...)) -> I
Create a multidimensional index I
, which can be used for indexing a multidimensional array A
. In particular, A[I]
is equivalent to A[i,j,k...]
. One can freely mix integer and CartesianIndex
indices; for example, A[Ipre, i, Ipost]
(where Ipre
and Ipost
are CartesianIndex
indices and i
is an Int
) can be a useful expression when writing algorithms that work along a single dimension of an array of arbitrary dimensionality.
A CartesianIndex
is sometimes produced by eachindex
, and always when iterating with an explicit CartesianIndices
.
jldoctest
julia> A = reshape(Vector(1:16), (2, 2, 2, 2))
2×2×2×2 Array{Int64,4}:
[:, :, 1, 1] =
1 3
2 4
[:, :, 2, 1] =
5 7
6 8
[:, :, 1, 2] =
9 11
10 12
[:, :, 2, 2] =
13 15
14 16
julia> A[CartesianIndex((1, 1, 1, 1))]
1
julia> A[CartesianIndex((1, 1, 1, 2))]
9
julia> A[CartesianIndex((1, 1, 2, 1))]
5
?CartesianIndices
search: CartesianIndices CartesianIndex
CartesianIndices(sz::Dims) -> R
CartesianIndices((istart:istop, jstart:jstop, ...)) -> R
Define a region R
spanning a multidimensional rectangular range of integer indices. These are most commonly encountered in the context of iteration, where for I in R ... end
will return CartesianIndex
indices I
equivalent to the nested loops
for j = jstart:jstop
for i = istart:istop
...
end
end
Consequently these can be useful for writing algorithms that work in arbitrary dimensions.
CartesianIndices(A::AbstractArray) -> R
As a convenience, constructing a CartesianIndices
from an array makes a range of its indices.
jldoctest
julia> foreach(println, CartesianIndices((2, 2, 2)))
CartesianIndex(1, 1, 1)
CartesianIndex(2, 1, 1)
CartesianIndex(1, 2, 1)
CartesianIndex(2, 2, 1)
CartesianIndex(1, 1, 2)
CartesianIndex(2, 1, 2)
CartesianIndex(1, 2, 2)
CartesianIndex(2, 2, 2)
julia> CartesianIndices(fill(1, (2,3)))
2×3 CartesianIndices{2,Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}}:
CartesianIndex(1, 1) CartesianIndex(1, 2) CartesianIndex(1, 3)
CartesianIndex(2, 1) CartesianIndex(2, 2) CartesianIndex(2, 3)
Linear index to cartesian index conversion exploits the fact that a CartesianIndices
is an AbstractArray
and can be indexed linearly:
jldoctest
julia> cartesian = CartesianIndices((1:3, 1:2))
3×2 CartesianIndices{2,Tuple{UnitRange{Int64},UnitRange{Int64}}}:
CartesianIndex(1, 1) CartesianIndex(1, 2)
CartesianIndex(2, 1) CartesianIndex(2, 2)
CartesianIndex(3, 1) CartesianIndex(3, 2)
julia> cartesian[4]
CartesianIndex(1, 2)
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
.
function f!(A::AbstractMatrix)
for CI in CartesianIndices(A)
i, j = CI.I
@show CI, i, j, A[CI]
A[CI] = i * j
end
end
f! (generic function with 1 method)
f!(A)
(CI, i, j, A[CI]) = (CartesianIndex(1, 1), 1, 1, 0.05491091194983966) (CI, i, j, A[CI]) = (CartesianIndex(2, 1), 2, 1, 0.4486226144552561) (CI, i, j, A[CI]) = (CartesianIndex(3, 1), 3, 1, 0.09302471246596777) (CI, i, j, A[CI]) = (CartesianIndex(1, 2), 1, 2, 0.9251127264830541) (CI, i, j, A[CI]) = (CartesianIndex(2, 2), 2, 2, 0.18320348558132737) (CI, i, j, A[CI]) = (CartesianIndex(3, 2), 3, 2, 0.5906888424135703) (CI, i, j, A[CI]) = (CartesianIndex(1, 3), 1, 3, 0.7925610544786179) (CI, i, j, A[CI]) = (CartesianIndex(2, 3), 2, 3, 0.309244948319527) (CI, i, j, A[CI]) = (CartesianIndex(3, 3), 3, 3, 0.017525501252093845) (CI, i, j, A[CI]) = (CartesianIndex(1, 4), 1, 4, 0.5520811765566824) (CI, i, j, A[CI]) = (CartesianIndex(2, 4), 2, 4, 0.020945370945398833) (CI, i, j, A[CI]) = (CartesianIndex(3, 4), 3, 4, 0.5120912274896219)
A
3×4 Array{Float64,2}: 1.0 2.0 3.0 4.0 2.0 4.0 6.0 8.0 3.0 6.0 9.0 12.0