# Convert between indexing formats

**URL:** <https://discourse.julialang.org/t/convert-between-indexing-formats/91658>\
**Category:** New to Julia\
**Tags:** question, cartesianindices\
**Created:** [December 14, 2022, 10:40pm UTC](https://discourse.julialang.org/t/convert-between-indexing-formats/91658 "2022-12-14T22:40:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![smartalecH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smartalech/32/32379_2.png) [@smartalecH](https://discourse.julialang.org/u/smartalecH)\
**Post date:** [December 14, 2022, 10:40pm UTC](https://discourse.julialang.org/t/convert-between-indexing-formats/91658/1 "2022-12-14T22:40:53Z")

</div>

Julia’s `eachindex` function makes it really easy to iterate over multidimensional arrays. Sometimes, however, I need access to not just the sequential index as described in memory (an Int) but also the individual dimension indices that are common of typical (e.g. C-like) for loops (e.g. `(ix, iy, iz)`).

Is there an easy way to go back and forth between index formats given a particular array? For example, something like

```julia
A = rand(2,3)

for i in eachindex(A)
   index_tuple = convert_index(A,i) # would give me (1,1), (2,1), (1,2), ...
end

```

(I see in the help there’s a way to get a `CartesianIndex` using a `view`, but then I lose my original index)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 14, 2022, 10:49pm UTC](https://discourse.julialang.org/t/convert-between-indexing-formats/91658/2 "2022-12-14T22:49:25Z")

</div>

Maybe:

```julia
CartesianIndices(A)[i]

```

---

<div class="post-metadata">

**Author:** ![smartalecH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smartalech/32/32379_2.png) [@smartalecH](https://discourse.julialang.org/u/smartalecH)\
**Post date:** [December 14, 2022, 10:50pm UTC](https://discourse.julialang.org/t/convert-between-indexing-formats/91658/3 "2022-12-14T22:50:59Z")

</div>

Aha! You’re right!

Turns out this is in the manual too: [Multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#Linear-indexing)

Thank you!
