> markov1 <- function(x,pi,P,n){ # x = vector [1 2 3 4] representing A, C, G, T, respectively # pi = the probability distribution for X0: (1x4 row vector) # P = transition matrix (4x4) # n = length of simulated sequence # Initialize vector to contain simulated sequence mg <- rep(0,n) # Produce initial element mg[1] <- sample(x,1,replace=TRUE,pi) for(k in 1:(n-1)){ mg[k+1]<-sample(x,1,replace=T,P[mg[k],]) } return(mg) }