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
A <- matrix(1:15, nrow=3)
then I want an output of the form
rbind(A, A, A, ...) #n times.
Obviously, there are many ways to construct such a large matrix, for example using a for loop or apply 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 rep function, but haven’t found a way to arrange the output of rep in a matrix of the desired format.
The solution
do.call("rbind", replicate(n, A, simplify=F))
is also too inefficient because rbind is used too often in this case. (Then, about 30% of the total runtime of my program are spent performing the rbinds.)
Does anyone know a better solution?
Two more solutions:
The first is a modification of the example in the question
The second involves unrolling the matrix, replicating it, and reassembling it.
Since efficiency is what was requested, benchmarking is necessary
which gives:
So the fastest is the raw
rbindcall, but that assumesnis fixed and known ahead of time. Ifnis not fixed, then the fastest isdo.call("rbind", rep(list(A), n). These were for a 3×5 matrix and 10 replications. Different sized matrices might give different orderings.EDIT:
For n=600, the results are in a different order (leaving out the explicit
rbindversion):giving
If you include the explicit
rbindversion, it is slightly faster than thedo.call("rbind", rep(list(A), n))version, but not by much, and slower than either theapplyormatrixversions. So a generalization to arbitraryndoes not require a loss of speed in this case.