Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|cite|improve this question
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.

share|cite|improve this answer
Sorry if I misunderstand - but this is solving FOR index, right? I am asking about if I already know index and the dimensions, how can I get i, j, k – Derek Jan 31 '11 at 23:32
I like to write that as $i + R(j + C(k + D(\ell + \cdots)))$. – Rahul Jan 31 '11 at 23:34
@Derek: see my addition – Ross Millikan Jan 31 '11 at 23:50
@Rahul: a good way to show the pattern – Ross Millikan Jan 31 '11 at 23:51
@Ross thanks for the help. I think this has me on the right track but I am still not getting exactly what I was looking for I guess. My problem is that I am processing an image, and each pixel has a 3x3 matrix associated with it. So my full data array is rowscols*9. I have to populate the 9 values of that 3x3 with data coming from a 2D dataset that is rowscols in size. I guess I am missing the translation between the 3D and 2D, so that for an index in the 3D set, I get the corresponding row/column from the 2D data set. – Derek Feb 1 '11 at 0:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.