Makes sense to me. I think you need to read this explanation over and over until you understand it: "Each element of IDX contains the linear index of the nearest nonzero pixel of BW." Then realize what the linear indexes are for each pixel. Any pixel in the white band will have that pixel as it's closest pixel, and any pixel in the big black band at the bottom will have the pixel at the bottom of the white band directly above it as the closest pixel, no matter what row you're in. For example, row 90 is the closest row no matter if you're in row 100, row 200, row 300, etc. And it goes up as you go from left to right because the column numbers increase from left to right. For example, the pixel at (90,100) has one linear index (=500*99+90) while the pixel next to it has a linear index 500 higher (=500*99+90), and they keep increasing as you go from left to right.
Do you think you have a need for this functionality?