It works like so:
julia> for i in eachindex(A)
@show A[i]
end
...
I.e. the `i` is to be used as sole index into the array. What `i` is
exactly depends on the type of the array. It's either just a Int or a
`CartesianIndex` has a field `I`. But you should not access `I`
directly, that is private to `CartesianIndex`.
Have a look at
help?> eachindex
and if you want to know what else one can do with CartesianIndex:
http://www.juliabloggers.com/multidimensional-algorithms-and-iteration/
On Thu, 2016-02-11 at 10:56, Evan <[email protected]> wrote:
> Hi, I am trying to understand how to use eachindex with an array. For example,
> I can do:
>
> julia> A = zeros(Int, (3,2))
> 3x2 Array{Int64,2}:
> 0 0
> 0 0
> 0 0
>
> followed by:
> julia> for iter in eachindex(A)
> @show iter.I[1], iter.I[2]
> @show A[iter]
> end
> ERROR: type Int64 has no field I
> [inlined code] from show.jl:127
> in anonymous at no file:0
>
>
>
> However, using:
> julia> A = sprand(2, 3, 0.5)
> 2x3 sparse matrix with 2 Float64 entries:
> [1, 1] = 0.599423
> [2, 2] = 0.340233
>
> julia> for iter in eachindex(A)
> @show iter.I[1], iter.I[2]
> @show A[iter]
> end
> (iter.I[1],iter.I[2]) = (1,1)
> A[iter] = 0.5994230074532017
> (iter.I[1],iter.I[2]) = (2,1)
> A[iter] = 0.0
> (iter.I[1],iter.I[2]) = (1,2)
> A[iter] = 0.0
> (iter.I[1],iter.I[2]) = (2,2)
> A[iter] = 0.3402329840051479
> (iter.I[1],iter.I[2]) = (1,3)
> A[iter] = 0.0
> (iter.I[1],iter.I[2]) = (2,3)
> A[iter] = 0.0
>
> works just fine. I'm sure it's something simple, but at this stage would
> appreciate some help.