close all
clear all
clc
load('path.mat');
load(fullfile('data matlab','VST_MATICE.mat'));
fprintf('||| TEST NA EXTREMNI HODNOTY ||| \n')
for misto_n = 1:3
    switch misto_n
        case 1
            misto = mat_AVG.AF;
            ID_misto = mat_AVG.ID_AF;
            poc_velikost_AF = size(misto,1);
        case 2
            misto = mat_AVG.AP;
            ID_misto = mat_AVG.ID_AP;
            poc_velikost_AP = size(misto,1);
        case 3
            misto = mat_AVG.ATP;
            ID_misto = mat_AVG.ID_ATP;
            poc_velikost_ATP = size(misto,1);
    end
    ID_vyradit_max = []; ID_vyradit_min = []; vektor = [];
    for index=1:6
        clear vektor;
        vektor(:,1) = 1:size(misto,1);
        vektor(:,2) = misto(:,index);
        index_max = find(vektor(:,2)==max(vektor(:,2)),1);
        index_min = find(vektor(:,2)==min(vektor(:,2)),1);
        test = 3.6; %3.4 pro N=120
        stredni = mean(vektor(:,2));
        sigma = sqrt(var(vektor(:,2)));
        statistika_max = (vektor(index_max,2)-stredni)/sigma;
        statistika_min = (stredni-vektor(index_min,2))/sigma;

        maxim=0;
        minim=0;
        H0 = statistika_max>=test;
        H1 = statistika_min>=test;

        while or(H0,H1) 
            if H0
                maxim=maxim+1;
                ID_vyradit_max(maxim,index)=vektor(index_max,1);
                vektor(index_max,:)=[];
                stredni = mean(vektor(:,2));
                sigma = sqrt(var(vektor(:,2)));
                index_max = find(vektor(:,2)==max(vektor(:,2)),1);
                statistika_max = (vektor(index_max,2)-stredni)/sigma;
                H0 = statistika_max>=test;
            elseif H1
                minim=minim+1;
                ID_vyradit_min(minim,index)=vektor(index_min,1);
                vektor(index_min,:)=[];
                stredni = mean(vektor(:,2));
                sigma = sqrt(var(vektor(:,2)));
                index_min = find(vektor(:,2)==min(vektor(:,2)),1);
                statistika_min = (stredni-vektor(index_min,2))/sigma;
                H1 = statistika_min>=test;
            end
        end 
    end

    ID_vyradit_min(ID_vyradit_min==0)=[];
    ID_vyradit_max(ID_vyradit_max==0)=[];
    ID_vyradit=sort([ID_vyradit_min(ID_vyradit_min>0),ID_vyradit_max(ID_vyradit_max>0)]);
    ID_vyradit = ID_vyradit';
    zaznam = 0; vicekrat = [];
    for index=2:length(ID_vyradit)
        if ID_vyradit(index)==ID_vyradit(index-1)
            zaznam=zaznam+1;
            vicekrat(zaznam)= index;
        end
    end
    ID_vyradit(vicekrat,:)=[];
    ID_vyradit(:,2)=ID_misto(ID_vyradit,1);
    ID_vyradit(:,3)=ID_misto(ID_vyradit(:,1),3);
    vyradit = [];
    for index=1:size(ID_vyradit,1)
        vyradit = [vyradit; find(ID_misto(:,1)==ID_vyradit(index,2)&ID_misto(:,3)==ID_vyradit(index,3))];
    end
    misto(vyradit,:) = [];
    ID_misto(vyradit,:) = [];
    switch misto_n
        case 1
            mat_AF = misto;
            ID_mat_AF = ID_misto;
        case 2
            mat_AP = misto;
            ID_mat_AP = ID_misto;        
        case 3
            mat_ATP = misto;
            ID_mat_ATP = ID_misto;
    end
end

%% Vytvoreni mnoziny LEG
prvni_pac = min(ID_mat_AF(:,1)); posledni_pac = max(ID_mat_AF(:,1));
pacienti = prvni_pac:posledni_pac;
pacient_LEG =[]; zaznam = 0;
for index=1:length(pacienti)
    for strana=1:2 
        zaznam = zaznam+1;
        AF = find(ID_mat_AF(:,1)==pacienti(index)&ID_mat_AF(:,3)==strana);
        AP = find(ID_mat_AP(:,1)==pacienti(index)&ID_mat_AP(:,3)==strana);
        ATP = find(ID_mat_ATP(:,1)==pacienti(index)&ID_mat_ATP(:,3)==strana);
        if isempty(AF); AF = 0; end
        if isempty(AP); AP = 0; end
        if isempty(ATP); ATP = 0; end

        pacient_LEG(1,zaznam) = AF;
        pacient_LEG(2,zaznam) = AP;
        pacient_LEG(3,zaznam) = ATP;
    end
end
pacient_LEG_zero = pacient_LEG(1,:).*pacient_LEG(2,:).*pacient_LEG(3,:);
pacient_LEG(:,pacient_LEG_zero==0) = []; 
mat_LEG = [mat_AF(pacient_LEG(1,:),:),mat_AP(pacient_LEG(2,:),:),mat_ATP(pacient_LEG(3,:),:)];
ID_mat_LEG = [ID_mat_AF(pacient_LEG(1,:),1:2),ID_mat_AP(pacient_LEG(2,:),2),ID_mat_ATP(pacient_LEG(3,:),2:4)];
           
%% Ulozeni do struktury
mat_AVG_test.AF = mat_AF;
mat_AVG_test.ID_AF = ID_mat_AF;
mat_AVG_test.AP = mat_AP;
mat_AVG_test.ID_AP = ID_mat_AP;
mat_AVG_test.ATP = mat_ATP;
mat_AVG_test.ID_ATP = ID_mat_ATP;
mat_AVG_test.LEG = mat_LEG;
mat_AVG_test.ID_LEG = ID_mat_LEG;

%% Pocet odstranenych zaznamu
vyrazeno(1) = poc_velikost_AF-size(mat_AF,1);
vyrazeno(2) = poc_velikost_AP-size(mat_AP,1);
vyrazeno(3) = poc_velikost_ATP-size(mat_ATP,1);
vyrazeno(4) = sum(vyrazeno(1:3));
%% Vypis a ulozeni dat
fprintf('Po provedeni testu na extremni hodnoty bylo vynechano celkem %.0f zaznamu.\n',vyrazeno(4));
fprintf('Mnozina  AF obsahuje %3.0f zaznamu (%.0f parametru), vynechano %2.0f zaznamu.\n',size(mat_AF),vyrazeno(1));
fprintf('Mnozina  AP obsahuje %3.0f zaznamu (%.0f parametru), vynechano %2.0f zaznamu.\n',size(mat_AP),vyrazeno(2));
fprintf('Mnozina ATP obsahuje %3.0f zaznamu (%.0f parametru), vynechano %2.0f zaznamu.\n',size(mat_ATP),vyrazeno(3));
fprintf('\n');
fprintf('Vytvorena mnozina LEG obsahujici %3.0f zaznamu (%2.0f parametru).\n',size(mat_LEG));

save(fullfile(PathName2,FileName3),'-append','mat_AVG_test');
save(('path.mat'),'-append','FileName3');



