I would like to estimate what is essentially a transition matrix, where M[i,j] = P(go to state j | start in state i). If there are no restrictions on the transition probabilities, I can use a dirichlet prior on each row of M

for(i in 1:C) {
M[i] ~ dirichlet(rep_vector(1, C));
}

where C is the number of rows and columns in M. However, I would now like to implement this where known elements of M are 0 (i.e. we know that some transitions are impossible), and where these elements are something that can be passed into the data from a user and are not hardcoded. How would you recommend writing this model in Stan?

IMHO, I suggest you perhaps create a design matrix D in which all elements are member of {0,1} and indicate the valid transitions, pairwise multiply M and D and then renormalise the rows to sum to 1.

Interestingâ€“so maybe have a matrix M_gamma where each each element has a gamma(1,1) distribution, multiply this by the matrix D, and then normalize the rows. The only thing I think I would have to think about is potentially modifying the priors for the M_gamma such that each row has the same amount of prior information.