# Converting an array of vectors into a matrix

I’m currently using ode_bdf_tol, which returns an array of T vectors, each of size N, where T is the number of timepoints and N the number of state variables. My data is a sum of a subset of the states, so I would like to do something like the following:

data {
real x[T];
}
transformed_data {
vector[N] w = [0, 0, 0, 1, 1, ..., 1]'; // select states to sum
}
model {
vector[N] sol[T];
sol[1:T] = ode_bdf_tol(...);  // an array of vectors
to_matrix(sol[1:T]) * w   // I want to do something like this
}

Conceptually, I’d like to ‘stack’ the array of vectors from ode_bdf_tol's output into a T x N matrix, which would enable the above and other vector/matrix operations. Is this something that’s currently supported?

Thanks!

We don’t have a function built for that (yet), but any implementation would just copy each vector to a row of a matrix, so you can just write your own in pure Stan:

functions {
matrix arr_to_matrix(vector[] vec_arr) {
// Get matrix dimensions (row, col)
int arr_dims[2] = dims(vec_arr);

// Declare matrix to copy vectors into
matrix[arr_dims[1], arr_dims[2]] mat;

// Copy vectors into matrix and return
for(r in 1:arr_dims[1]) {
mat[r] = vec_arr[r]';
}

return mat;
}
}