\(zmax = 800; \  (*\ Total\ number\ of\ spectra*) 

          xmax = 10; \  (*intensity\ levels*) 

          ymax = 10; \  (*\ 
            channels\ *) 

 (*\ 
            Initialize\ lists\ *) 

spec =. \)
spec = Table[{z},  {z,  1,  zmax} ];
spec[[All]] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

specrand =. 
specrand = 
                Table[{z},  {z,  1,  zmax} ];
specrand[[All]] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

mat =. 
mat = 
                Table[{z},  {z,  1,  zmax} ];
mat[[All]] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

  
  

For[z = 1,z <= zmax, z++,
spec[[z, 1]] = z/zmax;
For[ y = 2,y <= ymax - 2, y++,
spec[[ 
                            z, (2.0 + 
                                Floor[1.11*(y*(z/zmax))])  ]] = 
                        1.1 ^(-1*((5 - y)^2))]]; 


  

For[z = 1, 
              z <= zmax,  z++, 
For[ y = 1, 
                y <= ymax,  y++, 
spec[[z, 
                      y]] = {N[ spec[[z, y]] ], 
                    N[ spec[[z, y]] ], 
                    N[ spec[[z, y]] ]} 
]];

Show[
              Graphics[RasterArray[Apply[RGBColor,  spec, {2}]],  
                ImageSize  -> {800, 800} ]]; 

(* Randomize spectral rows: This code can pick the same row multiple times, or miss rows, but it is simple method to scramble mock data .  
                Actual data would be input after this step 

            and below the next comment statement *)

For[z = 1, 
            z <= zmax,  z++, 
tomove = 
              Random[Integer,  {1, 
                  zmax}];  
specrand[[z]] =  
              spec[[tomove]]
 ];

   

For[z = 1,z <= zmax, z++,mat[[z]]=specrand[[z, All, 1]] ];

Show[
              Graphics[RasterArray[Apply[RGBColor,  specrand, {2}]],  
                ImageSize  -> {800, 800} ]]; 

  

mat[[1]] = {0, 0.04, 0, 0, 0, 0, 0, 0, 0, 0};    
  

\(For[
scan = 1, \ 
            scan\  < \ zmax, \ \(scan++\)\ , ssd = 10; 

            For[
z = scan + 1, 
              z < zmax, \(z++\), 
For\ [
y = 1, y ≤ 10, \(y++\), 
                If[y == 1, ssdtemp = 0, \ 
                  ssdtemp = 
                    ssdtemp + \((mat[\([scan, y]\)] - 
                            mat[\([z, 
                                y]\)])\)^2\ ]
]; 
If[ssdtemp ≤ \ ssd, ssd = ssdtemp; up = z; 
                moveup = 
                  mat[\([z]\)]\ ]
]; 
mat[\([up]\)] = \ 
              mat[\([scan + 1]\)]; 
mat[\([scan + 1]\)] = 
              moveup
]\);

  

ListPlot3D[mat]

  

For[z = 1, 
              z <= zmax,  z++, 
For[ y = 1, 
                y <= ymax,  y++, 
mat[[z, 
                      y]] = {N[ mat[[z, y]] ], 
                    N[ mat[[z, y]] ], 
                    N[ mat[[z, y]] ]} 
]];

Show[
              Graphics[RasterArray[Apply[RGBColor,  mat, {2}]],  
                ImageSize  -> {800, 800}]];