I read the stan functions reference, which has `to_matrix`

`(vector v, int m, int n)`

But I got an error when doing the following:

‘’’

to_matrix(Sigma[subject_starts[n]: subject_stops[n]], V[n], V[n]));

‘’’

The error is Exception: to_matrix(matrix): rows * columns (36) and vector size (6) must match in size. However, I am pretty sure that my vector Sigma has size 36 and V[n] is 6 for the first loop. Does anyone know why I get this error? Thanks!

please share your code, or relevant sections.

for code formating use three backquotes - ``` - before and after your code (not straight quotes)

My covariance Sigma has different sizes for different subjects, so I am declaring it as a vector first, and use to_matrix() to calculate the marginal likelihood.

```
generated quantities{
vector[cov_size] Sigma;
for (n in 1:N){
Sigma[cov_starts[n]: cov_stops[n]] = to_vector(B[subject_starts[n]: subject_stops[n]] * Theta * Theta' * B[subject_starts[n]: subject_stops[n]]' + diag_matrix(rep_vector(sigma_eps^2,V[n])));
log_lik_marg[n] = multi_normal_lpdf(Y[subject_starts[n]: subject_stops[n]] | zero_v[subject_starts[n]: subject_stops[n]],
to_matrix(Sigma[subject_starts[n]: subject_stops[n]], V[n], V[n]));
```

the expression `Sigma[subject_starts[n]: subject_stops[n]]`

is picking out a slice of length 6, according to the error message.

Sigma may be size 36, but the slice isn’t, according to the math library.

Oh I see. Thank you so much, I forgot to change the index for Sigma in the to_matrix() function. Now it works!