title: How to select rows from two matrices and multiply them
body: <p>I am trying to loop through the rows in two matrices and multiply them but each attempt produces the first error below, which is clear, and a second error on dimension mismatch from the last two attempts. I have tried to change the format of the extracted rows to a form that is acceptable to stan but I don't know how to coerce them to the relevant format. How do I select the rows and multiply them please?</p>
  
  <blockquote>
    <p>SYNTAX ERROR, MESSAGE(S) FROM PARSER:<br>
    No matches for:<br>
      row_vector * row_vector  </p>
    
    <p>Expression is ill formed.  </p>
    
    <hr>
  
  <pre><code>16:       for(i in 1:Ns) {
  17: //        yh[i, n] = xnew[n]* beta[i];
  18:         yh[i, n] = xnew[n]* beta[i, ];
                                          ^
  19: //        yh[i, n] = xnew[n]* row(beta, i);
  </code></pre>
    
    <hr>
  </blockquote>
  
  <p>My <code>rstan</code> code</p>
  
  <pre><code>library("rstan") 
  txt &lt;- 
    'data {
      int&lt;lower=0&gt; N;   
      int&lt;lower=0&gt; K;     
      int Ns;
      matrix[N, K] xnew;   
      matrix[Ns, K] beta;
    }
    parameters {
    }
    model {
    }
    generated quantities {
      matrix[Ns, N] yh;
  
      for(n in 1:N) {
        for(i in 1:Ns) {
  //        yh[i, n] = xnew[n]* beta[i];
            yh[i, n] = xnew[n]* beta[i, ];
  //        yh[i, n] = xnew[n]* row(beta, i);
  //        yh[i, n] = xnew[n,]* row(beta, i);
  //        yh[i, n] = to_vector(row(xnew, n))* to_matrix(row(beta, i));
  //        yh[i, n] = to_vector(row(xnew, n))* row(beta, i);         
        }  
      }
    }
  
  '
  stan_model(model_code=txt) 
  </code></pre>
  
  <p>For clarity this is what i am trying to do in base R</p>
  
  <pre><code>set.seed(1)
  Ns=10; N=2; K=3
  beta = matrix(rnorm(Ns*K), ncol=K)
  xnew = matrix(rnorm(N*K), ncol=K)
  
  yh=matrix(nr=Ns, nc=N)
  for(n in 1:N) {
    for(i in 1:Ns) {
      p = as.numeric(xnew[n, , drop=FALSE] %*% beta[i,])
      yh[i, n] = p
    }  
  }
  
  #tcrossprod(beta, xnew)
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>This seems to do the trick:</p>
  
  <pre><code>yh[i, n] = dot_product(row(xnew, n), row(beta, i));
  </code></pre>
  
  <p>But is there a way to calculate this without looping through each row? (I can't see anything at <a href="https://mc-stan.org/docs/2_18/functions-reference/dot-products-and-specialized-products.html" rel="nofollow noreferrer">https://mc-stan.org/docs/2_18/functions-reference/dot-products-and-specialized-products.html</a>)</p>