function viterbipfad=multiViterbi(tlength, TransM, init)
%% generates Vierbipath from transition Matrix, or if given a positiv
%% integer it generates a iid random stateseries
viterbipfad=zeros(1:tlength,1);
R = rand(tlength,1);
if nargin < 3
    init=ceil(length(TransM)*rand(1,1));
end

if max(size(TransM))==1
    States=TransM;
    a=0:1:States-1;
    for i=1:States
viterbipfad(find(R<(i*(1/States))&R>(i-1)*(1/States)),1)=a(i); %#ok<FNDSB>
    end

else
    States=size(TransM,2);
    viterbipfad(1)=init;
    cTransM=zeros(States);
    cTransM(:,States)=1;
    for i=1:States
        for m=1:States-1
         cTransM(i,m)=sum(TransM(i,1:m));
        end
    end

    for t=1:tlength
        viterbipfad(t+1)=find(cTransM(viterbipfad(t),:)>R(t),1);
    end

end