
clear;
clc;

tab1=importdata('cecha_x2.dat');% tablica danych dla cechy 4
tab2=importdata('cecha_x3.dat');% tablica danych dla cechy 5

new_tab1 = sort(tab1);
new_tab2 = sort(tab2);






%%%%%%%%%% zmienne pomocnicze wykorzystane do wyznaczenia zbiorow rozmytych%%%%%%%%%%%%%
smpom1 = 0;
smpom2 = 0;
smpom3 = 0;
smpom4 = 0;
licz1 = 0;
licz2 = 0;
licz3 = 0;
licz4 = 0;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%deklaracja 9 regul, gdzie symbol M - mala wartosc, S - srednia wratosc, D - duza wartosc
MM = [0,0,0]; 
MS = [0,0,0];
MD = [0,0,0];
SM = [0,0,0];
SS = [0,0,0];
SD = [0,0,0];
DM = [0,0,0];
DS = [0,0,0];
DD = [0,0,0];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


m1 = min(tab1);% najmniejsza wartosc dla zbioru cechy 4 
d1 = max(tab1);% najwieksza wartosc dla zbioru cechy 4
s1 = sum(tab1)/length(tab1);% wartosc odpowiadajaca sredniej arytmetycznej dla zbioru cechy 4 

m2 = min(tab2);% najmniejsza wartosc dla zbioru cechy 5 
d2 = max(tab2);% najwieksza wartosc dla zbioru cechy 5
s2 = sum(tab2)/length(tab2);% wartosc odpowiadajaca sredniej arytmetycznej dla zbioru cechy 5 



for i=1:135
    if tab1(i)<s1
        
        smpom1 = smpom1+tab1(i);
        licz1=licz1+1;
                
    else smpom1 = smpom1 + 0;
    end
end

ms1 =smpom1/licz1;

%********************************************************************

for i=1:135
    if tab1(i)>s1
        
        smpom2 = smpom2+tab1(i);
        licz2=licz2+1;
        
    else smpom2 = smpom2 + 0;
    end
end

sd1 = smpom2/licz2;

%**********************************************************************

for i=1:135
    if tab2(i)<s2
        smpom3 = smpom3+tab2(i);
        licz3=licz3+1;
    else smpom3 = smpom3 + 0;
    end
end

ms2 = smpom3/licz3;

%***********************************************************************

for i=1:135
    if tab2(i)>s2
        smpom4 = smpom4+tab2(i);
        licz4=licz4+1;
    else smpom4 = smpom4 + 0;
    end
end

sd2 = smpom4/licz4;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



for i = 1:40 % przypisanie pierwszych 40 elementow tablic do klasy I
    tab1(i,2) = 1;%nr klasy zawarty jest w drugiej kolumnie obu tablic
    tab2(i,2) = 1;
end

for i = 41:85 % przypisanie elementow od nr 41 do nr 85 tablic do klasy II
    tab1(i,2) = 2;
        tab2(i,2) = 2;
end

for i = 86:135 % przypisanie elementow od nr 86 do nr 135 tablic do klasy III
    tab1(i,2) = 3;
    tab2(i,2) = 3;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%zakwalifikowanie danych do jednego z 3 zbiorow%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%M-male wartosci - zbior nr 1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%S-srednie wartosci - zbior nr 2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%D-duze wartosci - zbiornr 3%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%nr zbioru, do ktorego zakwalifikowane sa dane zawarta jest w 3 kolumnie tablic%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i = 1:135
    
    if (tab1(i,1)<ms1)
        tab1(i,3) =1;
        
    elseif(tab1(i,1)>sd1)
            tab1(i,3) =3;
            
        else tab1(i,3) = 2;
        end
    end
    
    
for i = 1:135
    
  if (tab2(i,1)<ms2)
      tab2(i,3) = 1;
        
  elseif(tab2(i,1)>sd2)
          tab2(i,3) = 3;
            
      else tab2(i,3) = 2;
      end
  end
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i = 1:40
    
    if (tab1(i,3)==1 & tab2(i,3)==1)
        MM(1) = MM(1)+1;
        
    elseif(tab1(i,3)==1 & tab2(i,3)==2)
        MS(1) = MS(1)+1;
        
    elseif(tab1(i,3)==1 & tab2(i,3)==3)
        MD(1) = MD(1)+1;
        
    elseif(tab1(i,3)==2 & tab2(i,3)==1)
        SM(1) = SM(1)+1;
        
    elseif(tab1(i,3)==2 & tab2(i,3)==2)
        SS(1) = SS(1)+1;
        
    elseif(tab1(i,3)==2 & tab2(i,3)==3)
        SD(1) = SD(1)+1;
        
    elseif(tab1(i,3)==3 & tab2(i,3)==1)
        DM(1) = DM(1)+1;
    
    elseif(tab1(i,3)==3 & tab2(i,3)==2)
        DS(1) = DS(1)+1;
        
    else
        DD(1) = DD(1)+1;
        
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        
for i = 41:85
    
    if (tab1(i,3)==1 & tab2(i,3)==1)
        MM(2) = MM(2)+1;
        
    elseif(tab1(i,3)==1 & tab2(i,3)==2)
        MS(2) = MS(2)+1;
        
    elseif(tab1(i,3)==1 & tab2(i,3)==3)
        MD(2) = MD(2)+1;
        
    elseif(tab1(i,3)==2 & tab2(i,3)==1)
        SM(2) = SM(2)+1;
        
    elseif(tab1(i,3)==2 & tab2(i,3)==2)
         SS(2) = SS(2)+1;
         
    elseif(tab1(i,3)==2 & tab2(i,3)==3)
        SD(2) = SD(2)+1;
        
    elseif(tab1(i,3)==3 & tab2(i,3)==1)
        DM(2) = DM(2)+1;
    
    elseif(tab1(i,3)==3 & tab2(i,3)==2)
        DS(2) = DS(2)+1;
        
    else
        DD(2) = DD(2)+1;
        
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i = 86:135
    
    if (tab1(i,3)==1 & tab2(i,3)==1)
        MM(3) = MM(3)+1;
        
    elseif(tab1(i,3)==1 & tab2(i,3)==2)
        MS(3) = MS(3)+1;
        
    elseif(tab1(i,3)==1 & tab2(i,3)==3)
        MD(3) = MD(3)+1;
        
    elseif(tab1(i,3)==2 & tab2(i,3)==1)
        SM(3) = SM(3)+1;
        
    elseif(tab1(i,3)==2 & tab2(i,3)==2)
        SS(3) = SS(3)+1;
        
    elseif(tab1(i,3)==2 & tab2(i,3)==3)
        SD(3) = SD(3)+1;
        
    elseif(tab1(i,3)==3 & tab2(i,3)==1)
        DM(3) = DM(3)+1;
    
    elseif(tab1(i,3)==3 & tab2(i,3)==2)
        DS(3) = DS(3)+1;
        
    else
        DD(3) = DD(3)+1;
        
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%st_XX odpowiada wartosci stopnia spelnienia 

st_MM = stopien_sp(MM);
st_MS = stopien_sp(MS);
st_MD = stopien_sp(MD);
st_SM = stopien_sp(SM);
st_SS = stopien_sp(SS);
st_SD = stopien_sp(SD);
st_DM = stopien_sp(DM);
st_DS = stopien_sp(DS);
st_DD = stopien_sp(DD);


kl_MM = klasa(st_MM);
kl_MS = klasa(st_MM);
kl_MD = klasa(st_MD);
kl_SM = klasa(st_SM);
kl_SS = klasa(st_SS);
kl_SD = klasa(st_SD);
kl_DM = klasa(st_DM);
kl_DS = klasa(st_DS);
kl_DD = klasa(st_DD);



%**************************************************************************************
%****************************W N I O S K O W A N I E***********************************
%**************************************************************************************

%first = 56  % pierwsza cecha obiektu
%second = 227 % druga cecha obiektu



fprintf('\n1.WYLONIENIE REGUL:\n'); 

 sumMM=MM(1)+MM(2)+MM(3); 
 sumSM=SM(1)+SM(2)+SM(3); 
 sumDM=DM(1)+DM(2)+DM(3); 
 fprintf('\tRegula MM sklada sie z:\t');  
 fprintf('\tRegula SM sklada sie z:\t');  
 fprintf('\tRegula DM sklada sie z:\n');  
 fprintf('\t{1:%d/%d,2:%d/%d,3:%d/%d}\t',MM(1),sumMM,MM(2),sumMM,MM(3),sumMM);
 fprintf('\t{1:%d/%d,2:%d/%d,3:%d/%d}\t',SM(1),sumSM,SM(2),sumSM,SM(3),sumSM);
 fprintf('\t{1:%d/%d,2:%d/%d,3:%d/%d}\n\n',DM(1),sumDM,DM(2),sumDM,DM(3),sumDM);

 sumMS=MS(1)+MS(2)+MS(3); 
 sumSS=SS(1)+SS(2)+SS(3); 
 sumDS=DS(1)+DS(2)+DS(3); 
 
 fprintf('\tRegula MS sklada sie z:\t');  
 fprintf('\tRegula SS sklada sie z:\t');  
 fprintf('\tRegula DS sklada sie z:\n');  

 fprintf('\t{1:%d/%d,2:%d/%d,3:%d/%d}\t',MS(1),sumMS,MS(2),sumMS,MS(3),sumMS);
 fprintf('\t   {1:%d/%d,2:%d/%d,3:%d/%d}\t',SS(1),sumSS,SS(2),sumSS,SS(3),sumSS);
 fprintf('{1:%d/%d,2:%d/%d,3:%d/%d}\n\n',DS(1),sumDS,DS(2),sumDS,DS(3),sumDS);
 
 sumMD=MD(1)+MD(2)+MD(3); 
 sumSD=SD(1)+SD(2)+SD(3); 
 sumDD=DD(1)+DD(2)+DD(3); 
 
 fprintf('\tRegula MD sklada sie z:\t');  
 fprintf('\tRegula SD sklada sie z:\t');  
 fprintf('\tRegula DD sklada sie z:\n');  

 fprintf('\t{1:%d/%d,2:%d/%d,3:%d/%d}\t',MD(1),sumMD,MD(2),sumMD,MD(3),sumMD);
 fprintf('\t   {1:%d/%d,2:%d/%d,3%d/%d}\t',SD(1),sumSD,SD(2),sumSD,SD(3),sumSD);
 fprintf('\t{1:%d/%d,2:%d/%d,3:%d/%d}\n\n',DD(1),sumDD,DD(2),sumDD,DD(3),sumDD);

 
 fprintf('\n2.WNIOSKOWANIE \n');  
 fprintf('\nZakres wartosci dla 1 wsp %d-%d\n',round(m1)+1,round(d1)-1);  
 fprintf('\nZakres wartosci dla 2 wsp %d-%d\n\n',round(m2)+1,round(d2)-1);  
  
R1 = input('Podaj pierwsza wspolrzedna:');
R2 = input('Podaj droga wspolrzedna:');

x1 = R1;
y1 = R2;

if(x1>=m1 & x1<=d1 & y1 >=m2 & y1<=d2)

iksy_11 = [m1 s1];
iksy_12 = [s1 d1];
iksy_21 = [m2 s2];
iksy_22 = [s2 d2];
val_1 = [1,0];
val_2 = [0,1];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[wsp11,ss11] = polyfit(iksy_11,val_1,1);
[wsp12,ss12] = polyfit(iksy_11,val_2,1);
[wsp13,ss13] = polyfit(iksy_12,val_1,1);
[wsp14,ss14] = polyfit(iksy_12,val_2,1);

[wsp21,ss21] = polyfit(iksy_21,val_1,1);
[wsp22,ss22] = polyfit(iksy_21,val_2,1);
[wsp23,ss23] = polyfit(iksy_22,val_1,1);
[wsp24,ss24] = polyfit(iksy_22,val_2,1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

y11 = wsp11(1,1)*x1 + wsp11(1,2);
y12 = wsp12(1,1)*x1 + wsp12(1,2);
y13 = wsp13(1,1)*x1 + wsp13(1,2);
y14 = wsp14(1,1)*x1 + wsp14(1,2);

y21 = wsp21(1,1)*y1 + wsp21(1,2);
y22 = wsp22(1,1)*y1 + wsp22(1,2);
y23 = wsp23(1,1)*y1 + wsp23(1,2);
y24 = wsp24(1,1)*y1 + wsp24(1,2);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


suma= [0 0 0];%suma konkluzji


if (m1<=x1 & x1<=s1 & m2<=y1 & y1<=s2)

    v_MM = min(y11,y21);
    suma = suma + (v_MM.*st_MM);
    
    v_SS = min(y12,y22);
    suma = suma + (v_SS.*st_SS);
    
    v_MS = min(y11,y22);
    suma = suma + (v_MS.*st_MS);
    
    v_SM = min(y12,y21);
    suma = suma + v_SM.*st_SM;
   

elseif (m1<=x1 & x1<=s1 & s2<=y1 & y1<=d2)
 
    v_MS = min(y11,y23);
    suma = suma + (v_MS.*st_MS);
    
    v_SS = min(y12,y23);
    suma = suma + (v_SS.*st_SS);
    
    v_MD = min(y11,y24);
    suma = suma + (v_MD.*st_MD);    
    
    v_SD = min(y12,y24);
    suma = suma + (v_SD.*st_SD);
    
    
elseif (s1<=x1 & x1<=d1 & m2<=y1 & y1<=s2)
 
    v_SM = min(y13,y21);
    suma = suma  + (v_SM.*st_SM);
    
    v_SS = min(y12,y23);
    suma = suma  + (v_SS.*st_SS);
    
    v_DM = min(y14,y21);
    suma = suma  +(v_DM.*st_DM);
      
    v_DS = min(y14,y22);
    suma = suma + (v_DS.*st_DS);
    
elseif(s1<=x1 & x1<=d1 & s1<=y1 & y1<=d2)
    
    v_SS = min(y13,y23);
    suma = suma + (v_SS.*st_SS);
    
    v_DS = min(y14,y23);
    suma = suma + (v_DS.*st_DS);
    
    v_SD = min(y13,y24);
    suma = suma + (v_SD.*st_SD);
    
    v_DD = min(y14,y24);
    suma = suma + ( v_DD.*st_DD);
end

 fprintf('\n3. KONKLUZJA WYPADKOWA (MAX) \n');  
 Obiekt_nalezy_do_klasy = klasa(suma)

else
    komunikat2 = 'wartosci poza zakresem'
end
   



