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 9183997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:56:19+00:00 2026-06-17T18:56:19+00:00

I have defined a custom function, like this: my.fun = function() { for (i

  • 0

I have defined a custom function, like this:

my.fun = function() {

      for (i in 1:1000) {
      ...
        for (j in 1:20) {
          ...
        }
      }

 return(output)

}

which returns an output matrix, output, composed by 1000 rows and 20 columns.

What I need to do is to repeat the function say 5 times and to store the five output results into a brand new matrix, say final, but without using another for-loop (this for making the code clearer, and also because in a second moment I would like to try to parallelize these additional 5 repetitions).

Hence final should be a matrix with 5000 rows and 20 columns (the rationale behind these 5 repetitions is that within the two for-loops I use, among other functions, sample).

I tried to use final <- replicate(5, my.fun()), which correctly computes the five replications, but then I have to “manually” put the elements into a brand new 5000 x 20 matrix.. is there a more elgant way to do so? (maybe using sapply()?). Many thanks

  • 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-17T18:56:20+00:00Added an answer on June 17, 2026 at 6:56 pm

    As is stands you probably have an array with three dimensions. If you wanted to have a list you would have added simplify=FALSE. Try this:

    do.call( rbind, replicate(5, my.fun(), simplify=FALSE ) )
    

    Or you can use aperm in the case where “final” is still an array:

    fun <- function() matrix(1:10, 2,5)
    final <- replicate( 2, fun() )
    > final
    , , 1
    
         [,1] [,2] [,3] [,4] [,5]
    [1,]    1    3    5    7    9
    [2,]    2    4    6    8   10
    
    , , 2
    
         [,1] [,2] [,3] [,4] [,5]
    [1,]    1    3    5    7    9
    [2,]    2    4    6    8   10
    
    > t( matrix(aperm(final, c(2,1,3)), 5,4) )
         [,1] [,2] [,3] [,4] [,5]
    [1,]    1    3    5    7    9
    [2,]    2    4    6    8   10
    [3,]    1    3    5    7    9
    [4,]    2    4    6    8   10
    

    There may be more economical matrix operations. I just haven’t discovered one yet.

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

Sidebar

Related Questions

I have defined a custom struct which I need to send over to another
I have view on which I defined a custom tagName. I need to be
I have a simple custom exception defined like like the following but I don't
We have a custom made server control which several properties defined in it. <cc1:DropdownCheck
I have a custom EL function defined as follows: <function> ... <function-signature> java.lang.String getAsText(com.test.Outerclass.InnerEnum)
I have a custom function defined that extracts part of an address from a
I have defined custom shortcut in General -> Key dialog for Show Whitespace character.
I have defined a custom list template type for SharePoint . I install it
I have defined a custom IPrincipal and custom IIdentity based on a website that
I have defined a custom file type with these lines: syn region SubSubtitle start=+=+

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.