function [prior1, transmat1, mu1, Sigma1, mixmat1] = treningMGS(data, broj_trening_podataka, Mix, States)
%mixture gaussian sa smjer + broj prstiju 
O = 2; %length of vector
T = 35; %length of vector sequence, broj frammeova
nex = broj_trening_podataka; %number of vector sequences, za treniranje, koliko opazanja
% data = randn(O,T,nex);
podatci=data;

%podatak
for i = 1:nex
    for j=2:36
        y_dist = podatci{i}(j,2)-podatci{i}(j-1,2);
        x_dist = podatci{i}(j,1)-podatci{i}(j-1,1);
        if x_dist == 0
            x_dist = 0.00000001;
        end
        a = atand(y_dist/x_dist)+0.01;
        if y_dist < 0 && x_dist > 0
            a = a + 360;
        elseif (y_dist < 0 && x_dist < 0) || (y_dist > 0 && x_dist < 0)
            a = a + 180;
        end
        
        testdata = round(a/20);
        if testdata == 0
            testdata = 1;
        elseif testdata == -4
            testdata = 14;
        end
        
%         if i == 21
%             podataktocan(1,j-1)=testdata;
%             podataktocan(2,j-1)=podatci{i}(j,3);
%             
%             if podataktocan(2,j-1)>5
%                 podataktocan(2,j-1)=5;
%             end
%             if podataktocan(2,j-1)==0
%                 podataktocan(2,j-1)=6;
%             end
%         elseif i == 22
%             podatakkrivi(1,j-1)=testdata;
%             podatakkrivi(2,j-1)=podatci{i}(j,3);
%             if podatakkrivi(2,j-1)==0
%                 podatakkrivi(2,j-1)=20;
%             end
%             if podatakkrivi(2,j-1)>5
%                 podatakkrivi(2,j-1)=5;
%             end
%         else
            podatak(1,j-1,i)=testdata;
            podatak(2,j-1,i)=podatci{i}(j,3);
            
            if podatak(2,j-1,i)>5
                podatak(2,j-1,i)=5;
            end
            if podatak(2,j-1,i)==0
                podatak(2,j-1,i)=6;
            end
%         end
        
    end
end

M =Mix; %mixture of 2 gaussians
Q = States;  %the number of states
left_right = 0;

prior0 = normalise(rand(Q,1));
transmat0 = mk_stochastic(rand(Q,Q));

[mu0, Sigma0] = mixgauss_init(Q*M, reshape(podatak, [O T*nex]), 'spherical');
mu0 = reshape(mu0, [O Q M]);
Sigma0 = reshape(Sigma0, [O O Q M]);
mixmat0 = mk_stochastic(rand(Q,M));


%poboljsavanje matrica

[LL, prior1, transmat1, mu1, Sigma1, mixmat1] = mhmm_em(podatak, prior0, transmat0, mu0, Sigma0, mixmat0, 'max_iter', 7);
%MGSnex100sphericalLR0(M,Q)=LL(end);


%testiranje sa nizom

%logliktocan = mhmm_logprob(podataktocan, prior1, transmat1, mu1, Sigma1, mixmat1);
%loglikkrivi = mhmm_logprob(podatakkrivi, prior1, transmat1, mu1, Sigma1, mixmat1);
end