Search code examples
arraysmatrixvectorizationbayesianstan

Efficiently transforming NxJ matrix into an N-long array with J numbers


I'm doing some Bayesian analysis using Stan and I'm trying to make my code more efficient.

In my Stan model string, I have a variable that is an NxJ matrix. It is declared this way to make use of quick matrix operations and assignments.

However, in the final modeling step (assigning a distribution), I need to transform this NxJ matrix into an N-long array that contains J real values in each of the array's elements.

In other words, I want the following transformation:

matrix[N,J] x;
vector[J] y[N];

for (i in 1:N)
    for (j in 1:J)
        y[i][j] = x[i,j]

Is there any way to do this in a vectorized way without for loops?

Thank you!!!!


Solution

  • No. Loops are very fast in Stan. The only reason to vectorize for speed is if there are derivatives involved. You could shorten it a bit to

    for (n in 1:N)
      y[n] = x[n]';
    

    but it wouldn't be any more efficient.

    I should qualify this by saying that there is one inefficiency here, which is lack of memory locality. If the matrices are large, they'll be slow to traverse by row because internally they are stored column-major.