function [out2,out3]=simulate_markov_label(P,n,initial_state,labels)
%SIMULATE_MARKOV takes a markov transition matrix P and generates a
%simulation of the chain for n steps.  It begins the process in the state
%given by initial_state
out=zeros(1,n);
out2=cell(1,n);
out(1)=initial_state;
out2(1)=labels(out(1));
out3=labels(out(1));
for i=2:n
    p=rand();
    tmp=cumsum(P(out(i-1),:));
    out(i)=find(p<tmp,1,'first');
    out2(i)=labels(out(i));
    out3=strcat(out3,out2(i));
end