# calculating coordinates from a flattened 3D array when you know the size, index and ordering

If I have a grid that I know is RxCxD in size, and I have number that corresponds to an element in that grid, and the grid is mapped in Row major format, how can I find out the ordered triplet that corresponds to the index that I know? that is

(i,j,k) = index

-
For a triplet, you need to specify more than just row major format. Which comes first, column or depth? – Willie Wong Jan 31 '11 at 22:32
depth comes last – Derek Jan 31 '11 at 23:36

If $i$ increases most quickly, then $j$, then $k$ (so a 2x2x2 array would be 0,0,0 then 1,0,0, then 0,1,0 etc) and you start counting from 0, it is $i+Rj+RCk$. If you count from 1, it is $(i-1)+R(j-1)+RC(k-1)+1$
Going the other direction, if the element number is $N$, then $k=N/RC, j=(N-kRC)/R, i=N-kRC-jR$ where the divisions are integer. This assumes you start from 0.
I like to write that as $i + R(j + C(k + D(\ell + \cdots)))$. – Rahul Jan 31 '11 at 23:34