Using integer array to index into a vector


Hi all,

I would like to use an integer array to index into a vector while in a for loop. I have an example below to illustrate what I want:

int N;
int M;
int indices[N] ;
vector[M] mu_alpha;
vector[M] sd_alpha;

vector[N] alpha;

for(j in 1:N){
alpha[i]~normal(mu_alpha[ indices[j] ], sd_alpha[ indices[j] ]);


in.dat = list(
N = 14, 
M = 7, 
indices = c(1,1,2,2,3,3,4,4,5,5,6,6,7,7), 
mu_alpha = c(5,3,6,3,2,1,9),
mu_sd = c(1,2,1.1,2.5,1.2,1.4,2.2)

The data and the model are not real, but they represent what I am trying to do. Does Stan allow for indexing into vectors using an integer array, or should I used ragged arrays and the segment function to accomplish this? Thank you for your time.



That is legal but much worse than

y ~ normal(mu_alpha[indices], sd_alpha[indices]);


Thank you. This worked better than what I initially had.