Function to reshape a vector to array of vectors

For example, given a vector A = append_row( rep_vector(1, 5), rep_vector(2, 5) ), do we have an Rstan function that directly reshape A as a 1d array of vectors like B = { rep_vector(1, 5), rep_vector(2, 5) }?

This function is useful to prepare the third argument of the function “map_rect”.

It’ll also be helpful if we have a function that can directly reshape a matrix as 1d array of vector

> rstan::lookup("to_array_1d")
    StanFunction      Arguments ReturnType Page
650  to_array_1d   (int[...] a)      int[]   76
651  to_array_1d     (matrix m)     real[]   76
652  to_array_1d  (real[...] a)     real[]   76
653  to_array_1d (row_vector v)     real[]   76
654  to_array_1d     (vector v)     real[]   76

“to_array_1d” gives 1 dimensional array but not 1 dimensional array of vector.

Sorry, my example is not accurate. I mean a function to reshape 1 whole vector or matrix to 1 dimensional array of vectors

The answer to your question is probably “no”, but you can write whichever of these reshaping functions you want in Stan. Or create them in C++ and create a pull request and we can include them in Stan. I’m guessing from the second note that you want this:

vector[] to_array_of_vectors(matrix x) {
  vector[cols(x)] y[rows(x)];
  for (i in 1:rows(x))
    for (j in 1:cols(x))
      y[i, j] = x[i, j];
  return y;

Thank you for this suggestion. Let me try C++ solution