# Thread: Get matrix indexes from the value

1. ## Get matrix indexes from the value

Hello,

I have a 3x3x3 matrix with values hanging from 1 to 27.
The order is this:
Code:
m[0][0][0] = 1;
m[0][0][1] = 2;
m[0][0][2] = 3;
m[0][1][0] = 4;
m[0][1][1] = 5;
m[0][1][2] = 6;
.
.
.
m[1][0][0] = 10;
m[1][0][1] = 11;
m[1][0][2] = 12;
.
.
.
m[2][2][0] = 25;
m[2][2][1] = 26;
m[2][2][2] = 27;
Is there an easy way to compute the indexes given the value?
For example, I know that the first index can be found by dividing the value by 9.0:
Code:
const int firstIndex = ceil(value / 9.0) - 1;
What about the second and the third indexes? I couldn't find a pattern in the numbers, and I didn't want to iterate through the matrix (the indexes will be calculated many times).

The other option is to create arrays where the values will be indexes and the matrix indexes will be the values. I'm not sure if it is the best way though, so I'd really appreciate any advice on this.

2. Looking at the pattern, I'd say that for m[i][j][k] with value n:
i = (n - 1) / 9
j = ((n - 1) / 3) % 3
k = (n - 1) % 3

3. Originally Posted by laserlight
Looking at the pattern, I'd say that for m[i][j][k] with value n:
i = (n - 1) / 9
j = ((n - 1) / 3) % 3
k = (n - 1) % 3
Can that be said without even knowing m's type? I suppose they're ints but even then is padding an issue?

4. Originally Posted by SirPrattlepod
Can that be said without even knowing m's type? I suppose they're ints but even then is padding an issue?
Well, if the values are of a floating point type, then yeah, there would be a problem with those expressions since they won't be using integer division. But if they are of an integer type, then there is no problem since this is just a matter of mapping one integer to another based on the mathematical pattern, i.e., the inner indices vary one by one, then the middle ones vary three by three, then the outer ones vary nine by nine.

5. I guess I was confused by the strange question

6. indexes are just representation of number n-1 in notation with base 3 - so laserlight's formulas are just one of ways to convert the number to this notation...

7. That's it, thank you laserlight!