mergeHMM <-
function(hmm,G){

    reOrder = unlist(G)
  
    distribution = hmm@internVariables$distribution
    
    #merged tpm and inner state probs
    nuP = gamma2nuP(hmm@transitionMatrix,G)
    nu=nuP$nu
    pList=nuP$pList
    
    if(distribution=="mv_normal")
      out=setHMM(transitionMatrix=nu,
                 mu=hmm@stateParameters$mu[,reOrder],
                 sigma=hmm@stateParameters$sigma[,,reOrder],
                 probList = pList)
    if(distribution=="normal")
      out=setHMM(transitionMatrix=nu,
                 mu=hmm@stateParameters$mu[reOrder],
                 sigma=hmm@stateParameters$sigma[reOrder],
                 probList = pList)
    return(out)

}
