title: Efficiently replicate matrices in R
body: <p>I have a matrix and look for an efficient way to replicate it n times (where n is the number of observations in the dataset). For example, if I have a matrix A</p>
  
  <p><code>A &lt;- matrix(1:15, nrow=3)</code></p>
  
  <p>then I want an output of the form</p>
  
  <p><code>rbind(A, A, A, ...)   #n times</code>.</p>
  
  <p>Obviously, there are many ways to construct such a large matrix, for example using a <code>for</code> loop or <code>apply</code> or similar functions. However, the call to the "matrix-replication-function" takes place in the very core of my optimization algorithm where it is called tens of thousands of times during one run of my program. Therefore, loops, apply-type of functions and anything similar to that are not efficient enough. (Such a solution would basically mean that a loop over n is performed tens of thousands of times, which is obviously inefficient.) I already tried to use the ordinary <code>rep</code> function, but haven't found a way to arrange the output of <code>rep</code> in a matrix of the desired format. </p>
  
  <p>The solution 
  <code>do.call("rbind", replicate(n, A, simplify=F))</code>
  is also too inefficient because <code>rbind</code> is used too often in this case. (Then, about 30% of the total runtime of my program are spent performing the rbinds.)</p>
  
  <p>Does anyone know a better solution?</p>