# How to best iterate over k-dimensional collection, e.g. (n,k) matrix?

**URL:** <https://discourse.julialang.org/t/how-to-best-iterate-over-k-dimensional-collection-e-g-n-k-matrix/92328>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 30, 2022, 7:07pm UTC](https://discourse.julialang.org/t/how-to-best-iterate-over-k-dimensional-collection-e-g-n-k-matrix/92328 "2022-12-30T19:07:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lighfe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lighfe/32/38831_2.png) [@Lighfe](https://discourse.julialang.org/u/Lighfe)\
**Post date:** [December 30, 2022, 7:07pm UTC](https://discourse.julialang.org/t/how-to-best-iterate-over-k-dimensional-collection-e-g-n-k-matrix/92328/1 "2022-12-30T19:07:38Z")

</div>

I have a dataset with n rows and k columns I want to iterate over. What is the recommended way?

Currently I would first create a n x k Matrix and then do two nested for-loops. I heard column wise is best

```julia
for col in 1:k
   for row in 1:n
      ...
   end
end

```

I heard so that functions like eachindex() and objects like CartesianIndex exist and I’d love to understand how to use them better. Eachindex loses the row and col information. CartesianIndex I was not able to properly use.

(1) Which collection to choose. Is Matrix okay?  
(2) Are there more simple ways to iterate over a collection while maintaining (a) the row and col indices, (b) the matrix index, (c) both  
(3) What are Cartesian Indices and how/when to use them?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 30, 2022, 7:30pm UTC](https://discourse.julialang.org/t/how-to-best-iterate-over-k-dimensional-collection-e-g-n-k-matrix/92328/2 "2022-12-30T19:30:13Z")

</div>

These are the most idiomatic ways, I think:

```julia
julia> a = [1 2 3; 4 5 6]
2×3 Matrix{Int64}:
 1 2 3
 4 5 6

julia> for i in axes(a,2), j in axes(a,1)
           @show j, i, a[j,i]
       end
(j, i, a[j, i]) = (1, 1, 1)
(j, i, a[j, i]) = (2, 1, 4)
(j, i, a[j, i]) = (1, 2, 2)
(j, i, a[j, i]) = (2, 2, 5)
(j, i, a[j, i]) = (1, 3, 3)
(j, i, a[j, i]) = (2, 3, 6)

julia> for c in CartesianIndices(a)
           @show c[1], c[2], a[c]
       end
(c[1], c[2], a[c]) = (1, 1, 1)
(c[1], c[2], a[c]) = (2, 1, 4)
(c[1], c[2], a[c]) = (1, 2, 2)
(c[1], c[2], a[c]) = (2, 2, 5)
(c[1], c[2], a[c]) = (1, 3, 3)
(c[1], c[2], a[c]) = (2, 3, 6)

```

There is nothing wrong with your double loop, just be sure that the indexes are inbounds (something that is guaranteed by these alternatives)

ps: a matrix is definitely ok. Alternatively you may want to look at DataFrames package and companions, if you want something more sophisticated in terms of data manipulation.

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [December 30, 2022, 10:44pm UTC](https://discourse.julialang.org/t/how-to-best-iterate-over-k-dimensional-collection-e-g-n-k-matrix/92328/3 "2022-12-30T22:44:23Z")

</div>

Other possible options can be found here (with bonus benchmarking, although they are probably dated):

> [@How much can hard-coded indexing be avoided in Julia?](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699):
>
> I’ve noticed a trend toward Julia’s new keywords trending towards using keywords and functions instead of integer indexing in Julia. For example, first(array) instead of array[1], or eachindex(array) instead of 1:length(array). Is this an intended aim for Julia style-- to avoid integer indexes in code that can avoid it? How likely is it that in the future Julia code will not need to show its 1-based integer heritage in common written code?

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [December 30, 2022, 10:58pm UTC](https://discourse.julialang.org/t/how-to-best-iterate-over-k-dimensional-collection-e-g-n-k-matrix/92328/4 "2022-12-30T22:58:55Z")

</div>

See also: [OrdinalIndexing.jl](https://github.com/jishnub/OrdinalIndexing.jl)
