Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8722859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:30:03+00:00 2026-06-13T07:30:03+00:00

I have a matrix and look for an efficient way to replicate it n

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:30:03+00:00Added an answer on June 13, 2026 at 7:30 am

    Two more solutions:

    The first is a modification of the example in the question

    do.call("rbind", rep(list(A), n))
    

    The second involves unrolling the matrix, replicating it, and reassembling it.

    matrix(rep(t(A),n), ncol=ncol(A), byrow=TRUE)
    

    Since efficiency is what was requested, benchmarking is necessary

    library("rbenchmark")
    A <- matrix(1:15, nrow=3)
    n <- 10
    
    benchmark(rbind(A, A, A, A, A, A, A, A, A, A),
              do.call("rbind", replicate(n, A, simplify=FALSE)),
              do.call("rbind", rep(list(A), n)),
              apply(A, 2, rep, n),
              matrix(rep(t(A),n), ncol=ncol(A), byrow=TRUE),
              order="relative", replications=100000)
    

    which gives:

                                                     test replications elapsed
    1                 rbind(A, A, A, A, A, A, A, A, A, A)       100000    0.91
    3                   do.call("rbind", rep(list(A), n))       100000    1.42
    5  matrix(rep(t(A), n), ncol = ncol(A), byrow = TRUE)       100000    2.20
    2 do.call("rbind", replicate(n, A, simplify = FALSE))       100000    3.03
    4                                 apply(A, 2, rep, n)       100000    7.75
      relative user.self sys.self user.child sys.child
    1    1.000      0.91        0         NA        NA
    3    1.560      1.42        0         NA        NA
    5    2.418      2.19        0         NA        NA
    2    3.330      3.03        0         NA        NA
    4    8.516      7.73        0         NA        NA
    

    So the fastest is the raw rbind call, but that assumes n is fixed and known ahead of time. If n is not fixed, then the fastest is do.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 rbind version):

    A <- matrix(1:15, nrow=3)
    n <- 600
    
    benchmark(do.call("rbind", replicate(n, A, simplify=FALSE)),
              do.call("rbind", rep(list(A), n)),
              apply(A, 2, rep, n),
              matrix(rep(t(A),n), ncol=ncol(A), byrow=TRUE),
              order="relative", replications=10000)
    

    giving

                                                     test replications elapsed
    4  matrix(rep(t(A), n), ncol = ncol(A), byrow = TRUE)        10000    1.74
    3                                 apply(A, 2, rep, n)        10000    2.57
    2                   do.call("rbind", rep(list(A), n))        10000    2.79
    1 do.call("rbind", replicate(n, A, simplify = FALSE))        10000    6.68
      relative user.self sys.self user.child sys.child
    4    1.000      1.75        0         NA        NA
    3    1.477      2.54        0         NA        NA
    2    1.603      2.79        0         NA        NA
    1    3.839      6.65        0         NA        NA
    

    If you include the explicit rbind version, it is slightly faster than the do.call("rbind", rep(list(A), n)) version, but not by much, and slower than either the apply or matrix versions. So a generalization to arbitrary n does not require a loss of speed in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following data matrix, I want to iterate over this matrix and look
I have a matrix A which I want to convert into a data.frame of
let's say I have a matrix (array) like this example, but much larger: 0
I have data in a matrix in the following way: label 1 2 3
I have a matrix of coordinates (X,Y), and I want to animate them by
I have a matrix with three columns: county, date, and number of ED visits.
I've used the Matrix class a thousand times. I have an elementary grasp of
I have two matrix with the same number of columns, but with different number
I have a matrix A which is: A <- matrix(c(1:15), byrow=T, nrow=5) A [,1]
I have matrix (a) with (1:10),<10 x 1> double. I would like to copy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.