Eachindex / Tuple conversion


#1

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


#2

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


#3

I also had to try…

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

#4

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

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