Extracting intensity values from each pixel in a z stack

Hi everyone, I’m writing a short code to check for fluorescence beads alignment. The strategy I’m considering is to generate the coordinates of the intensity distribution from a single bead in a cropped field of view across z axis. My code below is able to 1) shoot out coordinates for the xy plane and the 2) corresponding brightest pixel within the same plane but it’s unable to loop to the next slice. Can someone please help point out why this is the case? Any advice is much appreciated.

getDimensions(width, height, channels, slices, frames);

for (z=0; z<slices; z++) {
for (x=0; x<width; x++) {
for (y=0; y<height; y++){
X = Array.concat(X, x);
Y = Array.concat(Y, y);
Z = Array.concat(Z, z);
Pix = Array.concat(Pix, getPixel(x,y));

        X1=Array.slice(X, 1, X.length);
        Y1=Array.slice(Y, 1, Y.length);
        Z1=Array.slice(Z, 1, Z.length);
        Pix1=Array.slice(Pix, 1, Pix.length);
        Array.getStatistics(Pix1, min, max, mean, stdDev);
        for (i=0; i<Pix1.length; i++) {
            if(Pix1[i] == max) {

           Array.show(X1, Y1, Z1, Pix1);