Inverse (Column) Row-Major Order Transformation
math
Solution
If the index is calculated as
offset = row + column*NUMROWS
then the inverse would be
row = offset % NUMROWS
column = offset / NUMROWS
where `%` is modulus, and `/` is integer division.
This assumes the first element is at offset 0, row 0 and column 0. If they start at 1, you would have to add or subtract 1 at appropriate places.
For higher dimensions, you will have to repeat this for every measure.
offset = x + WIDTH*(y + HEIGHT*(z + DEPTH*time));
and the inverse
x = offset % WIDTH
offset = offset / WIDTH
y = offset % HEIGHT
offset = offset / HEIGHT
z = offset % DEPTH
offset = offset / DEPTH
time = offset
You could also extract a specific coordinate:
z = (offset / (WIDTH * HEIGHT)) % DEPTH
Problem
Can anyone provide/refer to the inverse of the 'indices -> offset'* transformation for Multi-Dimensional Row-Major Order. Also, (pseudo)code would be appreciated. - http://en.wikipedia.org/wiki/Row-major_order To give an example, an simplification of the particular problem which prompted my question: I have a 3 dimensional data hierarchy, expressed in the space spanned by (a,b,c) where a, b, and c are integers larger or equal zero and less then N_a, N_b, and N_c. I want to express the data an one dimensional array. The "offset," In Row-Major Order, is then given as follows: ``` int offset(a, b, c){ return a*N_b*N_c + b*N_c + c; } ``` What is then the reverse transformation, i.e.: ``` int a(int offset); int b(int offset); int c(int offset); ``` Furthermore, how to i generalise this to N'th dimension indexation? The problem which prompted this question is of 5'th dimension. In case it matters, I am writing in c/c++.