Eachindex / Tuple conversion

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).

Its either ind2sub or sub2ind. Never know which is which… :smile:

1 Like

I also had to try…

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

Yes, but be cautious.

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

5 Likes

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