Divide a 3D matrix into multiple smaller 3D matrix I'm trying to build an extension method that will allow me to subdivide a huge array into much smaller ones. My current code is the following but it still takes about 2s to generate with an array of size 512x512x512 and the size of the sub cubes set to 32x32x32
public static T[][][][] Divide&lt;T&gt;(this T[][][] array, int xSize, int ySize, int zSize)
{
    int numberOfCubesInAxisX = array.Length / xSize;
    int numberOfCubesInAxisY = array[0].Length / ySize;
    int numberOfCubesInAxisZ = array[0][0].Length / zSize;
    T[][][][] arrayDivided = new T[numberOfCubesInAxisX * numberOfCubesInAxisY * numberOfCubesInAxisZ][][][];
    int index = 0;
    while (index &lt; arrayDivided.Length)
    {
        int xIndex = index / numberOfCubesInAxisZ / numberOfCubesInAxisX;
        int yIndex = index / numberOfCubesInAxisZ % numberOfCubesInAxisY;
        int zIndex = index % numberOfCubesInAxisZ;
        arrayDivided[index] = new T[xSize][][];
        for (int x = 0; x &lt; xSize; x++)
        {
            arrayDivided[index][x] = new T[ySize][];
            for (int y = 0; y &lt; ySize; y++)
            {
                arrayDivided[index][x][y] = new T[zSize];
                for (int z = 0; z &lt; zSize; z++)
                {
                    arrayDivided[index][x][y][z] = array[x + (xIndex * xSize)][y + (yIndex * ySize)][z + (zIndex * zSize)];
                }
            }
        }
        index++;
    }

    return arrayDivided;
}

If anyone have any clue on how to optimize it, i'll be glad to hear it!
Thanks!
Just a quick shot regarding performance.....
The code is calculating
xIndex * xSize xSize*ySize*zSize times
yIndex * ySize xSize*ySize*zSize times
zIndex * zSize xSize*ySize*zSize times
while it only should be calculated once for each iteration of the while loop.
In addition, x only changes in the first for loop and y only changes in the second for loop.