Code: Select all
Question:
How to find the position that has an element of an array / vector / multidimensional array, using a mathematical formula?
For example, in a 2D array of 4 x 4
A B C D
E F G H
I J K L
M N O P
A 1D 2D array = Array = ABCDEFGHIJKLMNOP
Position = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16. Ok!
Then, the position Rowx = 3, colx = 3, position = 11 & content = K
Formula: ((Rowx -1) * col) + colx
= ((3 - 1) * 4) + 3 = Position 11 = element = K
:::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::
Now in a 3D array = 4 x 4 x 4
1D = 3D = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ... 60 61 62 63 64
POSITION 63 = CONTENT = 63
level 1
1 2 3 4
5 7 7 8
9 10 11 12
13 14 15 16
level 2
17 18 19 20
21 22 23 24
25 26 27 28
29 30 31 32
level 3
33 34 35 36
37 38 39 40
41 42 43 44
45 46 47 48
level 4
49 50 51 52
53 54 55 56
57 58 59 60
61 62 63 64
Example: Row 4 x column 3 x Level 4 = 4x3x4 = 63 Position 63 = Element
3D position pa Formula:
((Rowx - 1) * col) + colx + ((rows * cols) * (level -1))
((4 - 1) * 4 * 4) + 3 + ((4 * 4) * (4 - 1)) = POSITION 63 = content = 63
:::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::
So my question is:
What is the formula to find the position within an array larger than 4 or 5 or 20?
Thanks"