2
$\begingroup$

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

  • 0
    For a triplet, you need to specify more than just row major format. Which comes first, column or depth?2011-01-31
  • 0
    depth comes last2011-01-31

1 Answers 1

1

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.

  • 0
    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, k2011-01-31
  • 0
    I like to write that as $i + R(j + C(k + D(\ell + \cdots)))$.2011-01-31
  • 0
    @Derek: see my addition2011-01-31
  • 0
    @Rahul: a good way to show the pattern2011-01-31
  • 0
    @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 rows*cols*9. I have to populate the 9 values of that 3x3 with data coming from a 2D dataset that is rows*cols 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.2011-02-01
  • 0
    @Derek: One approach is to make a 4 dimensional array RxCx3x3. The extension of what I gave should be obvious. But I don't see how you get nine values out of one in your data set.2011-02-01
  • 0
    i have 3 input data arrays of size rows*cols, and I am calculating a covariance matrix for each (row,col). The values for that calculation comes from the 3 input data arrays, and is stored for in the rows*cols*9 output data array2011-02-01
  • 0
    So you have 3*rows*cols inputs and are calculating 9*rows*cols outputs? This needs some thought beyond array indexing.2011-02-01