Divide a 3D matrix into multiple smaller 3D matrix
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.