function [priorE, transmatE, obsmatE] = treningDP(data, broj_trening_podataka, States)
%diskretni sa pozicijama + broj prstiju
O = 640*480*6; %velicina codebook-a
podatci=data;

for i = 1:broj_trening_podataka
    for j=1:36
            
            if podatci{i}(j,3)>5
                podatci{i}(j,3)=5;
            elseif podatci{i}(j,3)==0
                podatci{i}(j,3)=6;
            end
            
%             if i == 21 
%                podataktocan(j)=(podatci{i}(j,1)-1)*480*6 + (podatci{i}(j,2)-1)*6 + (podatci{i}(j,3)+1);
%             else
               podatak((i-1)*31+j)=(podatci{i}(j,1)-1)*480*6 + (podatci{i}(j,2)-1)*6 + (podatci{i}(j,3));
%             end
        
    end
end

Q = States;  %broj stanja
    % initial guess of parameters
    priorG = normalise(rand(Q,1));
    transmatG = mk_stochastic(rand(Q,Q));
    obsmatG = mk_stochastic(rand(Q,O));
    
    % improve guess of parameters using EM
    [LL, priorE, transmatE, obsmatE] = dhmm_em(podatak, priorG, transmatG, obsmatG, 'max_iter', 5);
    
    %DPnex20(Q)=LL(end);
    

%osiguranje da niti jedan clan matrice nije 0
for i = 1:(length(obsmatE)*size(obsmatE,1))
    if obsmatE(i) == 0
        obsmatE(i) = 2e-200;
    end
end
for i = 1:(length(transmatE)*size(transmatE,1))
    if transmatE(i) == 0
        transmatE(i) = 2e-200;
    end
end

%loglik = dhmm_logprob(podatatocan, priorE, transmatE, obsmatE)
%loglik = dhmm_logprob(podatakrivi, priorE, transmatE, obsmatE)

end