# Eachindex / Tuple conversion

**URL:** <https://discourse.julialang.org/t/eachindex-tuple-conversion/8443>\
**Category:** General Usage\
**Created:** [January 17, 2018, 9:12pm UTC](https://discourse.julialang.org/t/eachindex-tuple-conversion/8443 "2018-01-17T21:12:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tpoisot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tpoisot/32/19812_2.png) [@tpoisot](https://discourse.julialang.org/u/tpoisot)\
**Post date:** [January 17, 2018, 9:12pm UTC](https://discourse.julialang.org/t/eachindex-tuple-conversion/8443/1 "2018-01-17T21:12:33Z")

</div>

This may already be implemented, but I’ve failed to find it in the documentation.

If I have a two dimensional array, for which I know the index, is there a way to get the same information but in a tuple of position?

So for example index 4 in a 2x2 matrix would be `(2,2)`.

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [January 17, 2018, 9:20pm UTC](https://discourse.julialang.org/t/eachindex-tuple-conversion/8443/2 "2018-01-17T21:20:39Z")

</div>

Its either `ind2sub` or `sub2ind`. Never know which is which… 😄

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [January 17, 2018, 9:21pm UTC](https://discourse.julialang.org/t/eachindex-tuple-conversion/8443/3 "2018-01-17T21:21:49Z")

</div>

I also had to try…

```julia
julia> ind2sub(rand(2,2), 4)
(2, 2)

```

---

<div class="post-metadata">

**Author:** ![turtle](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@turtle](https://discourse.julialang.org/u/turtle)\
**Post date:** [January 17, 2018, 10:18pm UTC](https://discourse.julialang.org/t/eachindex-tuple-conversion/8443/4 "2018-01-17T22:18:54Z")

</div>

Yes, but be cautious.

According to [https://github.com/JuliaLang/julia/blob/master/NEWS.md](https://github.com/JuliaLang/julia/blob/master/NEWS.md)  
`sub2ind` and `ind2sub` are deprecated in favor of using `CartesianIndices` and `LinearIndices` (#24715).

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [January 17, 2018, 10:23pm UTC](https://discourse.julialang.org/t/eachindex-tuple-conversion/8443/5 "2018-01-17T22:23:07Z")

</div>

Good to know. IMO `CartesianIndices` and `LinearIndices` are much better names.
