clear;
%matriz de medias aritmeticas respecto la banda (columnas) y la clase (filas)
medias=[[115.277 70.372 106.012 107.380 209.479 116.039] ;[68.328 30.105 26.695 123.223 73.126 20.247] ;[111.320 65.533 98.197 108.677 179.319 77.505] ;[87.335 46.915 58.647 97.2129 148.860 65.172] ;[94.979 56.713 85.416 93.802 175.921 95.531] ;[95.527 50.447 65.560 79.705 134.636 74.407] ;[120.892 73.567 108.856 110.193 211.629 113.915] ;[121.061 74.219 111.784 114.038 206.530 103.853]];

%abre los ficheros
banda1='tm1.dat';
Fid=fopen(banda1,'r+b');
[F1,count]=fread(Fid,[1024,1024],'uint8');
fclose(Fid);

banda2='tm2.dat';
Fid=fopen(banda2,'r+b');
[F2,count]=fread(Fid,[1024,1024],'uint8');
fclose(Fid);

banda3='tm3.dat';
Fid=fopen(banda3,'r+b');
[F3,count]=fread(Fid,[1024,1024],'uint8');
fclose(Fid);

banda4='tm4.dat';
Fid=fopen(banda4,'r+b');
[F4,count]=fread(Fid,[1024,1024],'uint8');
fclose(Fid);

banda5='tm5.dat';
Fid=fopen(banda5,'r+b');
[F5,count]=fread(Fid,[1024,1024],'uint8');
fclose(Fid);

banda6='tm7.dat';
Fid=fopen(banda6,'r+b');
[F6,count]=fread(Fid,[1024,1024],'uint8');
fclose(Fid);

for(i=1:1024)
    for(j=1:512)
        for (k=1:8)
            a=F1(i,j)-medias(k,1);
            a=a+(F2(i,j)-medias(k,2));
            a=a+(F3(i,j)-medias(k,3));
            a=a+(F4(i,j)-medias(k,4));
            a=a+(F5(i,j)-medias(k,5));
            a=a+(F6(i,j)-medias(k,6));
            b(k)=sqrt(a);
        end
        [f,g]=min(b);
        clase(i,j)=g;
    end
end
Fid=fopen('clase.dat','w+b');
count=fwrite(Fid,clase,'uint8');
fclose(Fid);  
