Consider m = [1 2 3; 4 5 6; 7 8 9]
for idx in eachindex(m)
println(idx)
end
I was expecting it to print (1, 1) (2, 1), (3, 1) .... (1, 3), (2, 3), (3, 3)
but it prints 1, 2, ..., 9
.
What's the most elegant way to loop through all the indices of a multidimensional array?
What about
julia> for i in CartesianIndices(m)
println(Tuple(i))
end
(1, 1)
(2, 1)
(3, 1)
(1, 2)
(2, 2)
(3, 2)
(1, 3)
(2, 3)
(3, 3)
(You can access the tuple of subindices of i::CartseianIndex
with Tuple(i)
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With