kit*_*kun 4 arrays 3d multidimensional-array
我需要从1D阵列中提取4D位置.我可以看到2D和3D是怎么回事,但是我很难将头围绕在第四维度上.
对于2D:
int* array = new int[width * height];
int index = y * width + x;
int x = index / height
int y = index - x * height;
Run Code Online (Sandbox Code Playgroud)
对于3D:
int* array = new int[width * height * depth];
int index = z * width * height + y * width + z;
int x = index / (height * depth);
int y = index - (x * height * depth) / depth;
int z = index - (x * height * depth) - (y * depth);
Run Code Online (Sandbox Code Playgroud)
对于4D?
int* array = new int[width * height * depth * duration];
int index = w * width * height * depth + z * width * height + y * width + w;
int x = index / (height * depth * duration);
int y = ??
Run Code Online (Sandbox Code Playgroud)
索引公式由任何给定维度值与所有先前维度的乘积相乘得出.
Index = xn ( D1 * ... * D{n-1} ) + x{n-1} ( D1 * ... * D{n-2} ) + ... + x2 * D1 + x1
Run Code Online (Sandbox Code Playgroud)
所以对于4D
index = x + y * D1 + z * D1 * D2 + t * D1 * D2 * D3;
x = Index % D1;
y = ( ( Index - x ) / D1 ) % D2;
z = ( ( Index - y * D1 - x ) / (D1 * D2) ) % D3;
t = ( ( Index - z * D2 * D1 - y * D1 - x ) / (D1 * D2 * D3) ) % D4;
/* Technically the last modulus is not required,
since that division SHOULD be bounded by D4 anyways... */
Run Code Online (Sandbox Code Playgroud)
通式为形式
xn = ( ( Index - Index( x1, ..., x{n-1} ) ) / Product( D1, ..., D{N-1} ) ) % Dn
Run Code Online (Sandbox Code Playgroud)