Most efficient way to use values of Matrix as row indices to lookup values in another matrix in R












2















I am repeatetly drawing large matrices with random values from a Monte Carlo Simulation. As I explore a large parameter space, the simulation will most likely run for several days, therefore I am trying to find most efficient way to shave off as much time as possible. Consider the following code with a 500x18 Matrix as an example.



U = matrix(sample.int(500, size = 500*18, replace = TRUE), nrow = 500, ncol = 18)
X = matrix(nrow= 500, ncol = 18)
Marginals = matrix(runif(500*18, min = 0, max = 1),500,18)

for (i in 1:18){
for (k in 1:500){
X[k,i] = Marginals[U[k,i],i]

}
}


The randomly drawn values in U serve as the row index, while the col index is provided by column of the respective U.



I know that loops are usually not the R away, is there a more efficient way to use e.g. apply here?



By Yogos Suggesiton, the most efficient code can make due without the k loop:



U = matrix(sample.int(500, size = 500*18, replace = TRUE), nrow = 500, ncol = 18)
X = matrix(nrow= 500, ncol = 18)
Marginals = matrix(runif(500*18, min = 0, max = 1),500,18)

for (i in 1:18){

X[, i] <- Marginals[U[, i], i]

}









share|improve this question




















  • 1





    for (i in 1:18) X[, i] <- Marginals[U[, i], i]

    – jogo
    Nov 21 '18 at 12:00













  • eventually X <- replicate(18, sample(runif(500), repl=TRUE)) is equivalent to your code.

    – jogo
    Nov 21 '18 at 12:12











  • Thanks Yogo, the actual numbers in U are not uniformly distributed, but drawn by a more complex function (copula). I will edit your suggestion as the solution, thank you very much

    – Ozaki_san
    Nov 21 '18 at 12:16






  • 1





    I suggest to remove the answer from the question. Then please accept the answer which @jogo provided in order to indicate that you have found an acceptable solution.

    – Heikki
    Nov 21 '18 at 12:37
















2















I am repeatetly drawing large matrices with random values from a Monte Carlo Simulation. As I explore a large parameter space, the simulation will most likely run for several days, therefore I am trying to find most efficient way to shave off as much time as possible. Consider the following code with a 500x18 Matrix as an example.



U = matrix(sample.int(500, size = 500*18, replace = TRUE), nrow = 500, ncol = 18)
X = matrix(nrow= 500, ncol = 18)
Marginals = matrix(runif(500*18, min = 0, max = 1),500,18)

for (i in 1:18){
for (k in 1:500){
X[k,i] = Marginals[U[k,i],i]

}
}


The randomly drawn values in U serve as the row index, while the col index is provided by column of the respective U.



I know that loops are usually not the R away, is there a more efficient way to use e.g. apply here?



By Yogos Suggesiton, the most efficient code can make due without the k loop:



U = matrix(sample.int(500, size = 500*18, replace = TRUE), nrow = 500, ncol = 18)
X = matrix(nrow= 500, ncol = 18)
Marginals = matrix(runif(500*18, min = 0, max = 1),500,18)

for (i in 1:18){

X[, i] <- Marginals[U[, i], i]

}









share|improve this question




















  • 1





    for (i in 1:18) X[, i] <- Marginals[U[, i], i]

    – jogo
    Nov 21 '18 at 12:00













  • eventually X <- replicate(18, sample(runif(500), repl=TRUE)) is equivalent to your code.

    – jogo
    Nov 21 '18 at 12:12











  • Thanks Yogo, the actual numbers in U are not uniformly distributed, but drawn by a more complex function (copula). I will edit your suggestion as the solution, thank you very much

    – Ozaki_san
    Nov 21 '18 at 12:16






  • 1





    I suggest to remove the answer from the question. Then please accept the answer which @jogo provided in order to indicate that you have found an acceptable solution.

    – Heikki
    Nov 21 '18 at 12:37














2












2








2








I am repeatetly drawing large matrices with random values from a Monte Carlo Simulation. As I explore a large parameter space, the simulation will most likely run for several days, therefore I am trying to find most efficient way to shave off as much time as possible. Consider the following code with a 500x18 Matrix as an example.



U = matrix(sample.int(500, size = 500*18, replace = TRUE), nrow = 500, ncol = 18)
X = matrix(nrow= 500, ncol = 18)
Marginals = matrix(runif(500*18, min = 0, max = 1),500,18)

for (i in 1:18){
for (k in 1:500){
X[k,i] = Marginals[U[k,i],i]

}
}


The randomly drawn values in U serve as the row index, while the col index is provided by column of the respective U.



I know that loops are usually not the R away, is there a more efficient way to use e.g. apply here?



By Yogos Suggesiton, the most efficient code can make due without the k loop:



U = matrix(sample.int(500, size = 500*18, replace = TRUE), nrow = 500, ncol = 18)
X = matrix(nrow= 500, ncol = 18)
Marginals = matrix(runif(500*18, min = 0, max = 1),500,18)

for (i in 1:18){

X[, i] <- Marginals[U[, i], i]

}









share|improve this question
















I am repeatetly drawing large matrices with random values from a Monte Carlo Simulation. As I explore a large parameter space, the simulation will most likely run for several days, therefore I am trying to find most efficient way to shave off as much time as possible. Consider the following code with a 500x18 Matrix as an example.



U = matrix(sample.int(500, size = 500*18, replace = TRUE), nrow = 500, ncol = 18)
X = matrix(nrow= 500, ncol = 18)
Marginals = matrix(runif(500*18, min = 0, max = 1),500,18)

for (i in 1:18){
for (k in 1:500){
X[k,i] = Marginals[U[k,i],i]

}
}


The randomly drawn values in U serve as the row index, while the col index is provided by column of the respective U.



I know that loops are usually not the R away, is there a more efficient way to use e.g. apply here?



By Yogos Suggesiton, the most efficient code can make due without the k loop:



U = matrix(sample.int(500, size = 500*18, replace = TRUE), nrow = 500, ncol = 18)
X = matrix(nrow= 500, ncol = 18)
Marginals = matrix(runif(500*18, min = 0, max = 1),500,18)

for (i in 1:18){

X[, i] <- Marginals[U[, i], i]

}






r matrix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 12:18







Ozaki_san

















asked Nov 21 '18 at 11:21









Ozaki_sanOzaki_san

133




133








  • 1





    for (i in 1:18) X[, i] <- Marginals[U[, i], i]

    – jogo
    Nov 21 '18 at 12:00













  • eventually X <- replicate(18, sample(runif(500), repl=TRUE)) is equivalent to your code.

    – jogo
    Nov 21 '18 at 12:12











  • Thanks Yogo, the actual numbers in U are not uniformly distributed, but drawn by a more complex function (copula). I will edit your suggestion as the solution, thank you very much

    – Ozaki_san
    Nov 21 '18 at 12:16






  • 1





    I suggest to remove the answer from the question. Then please accept the answer which @jogo provided in order to indicate that you have found an acceptable solution.

    – Heikki
    Nov 21 '18 at 12:37














  • 1





    for (i in 1:18) X[, i] <- Marginals[U[, i], i]

    – jogo
    Nov 21 '18 at 12:00













  • eventually X <- replicate(18, sample(runif(500), repl=TRUE)) is equivalent to your code.

    – jogo
    Nov 21 '18 at 12:12











  • Thanks Yogo, the actual numbers in U are not uniformly distributed, but drawn by a more complex function (copula). I will edit your suggestion as the solution, thank you very much

    – Ozaki_san
    Nov 21 '18 at 12:16






  • 1





    I suggest to remove the answer from the question. Then please accept the answer which @jogo provided in order to indicate that you have found an acceptable solution.

    – Heikki
    Nov 21 '18 at 12:37








1




1





for (i in 1:18) X[, i] <- Marginals[U[, i], i]

– jogo
Nov 21 '18 at 12:00







for (i in 1:18) X[, i] <- Marginals[U[, i], i]

– jogo
Nov 21 '18 at 12:00















eventually X <- replicate(18, sample(runif(500), repl=TRUE)) is equivalent to your code.

– jogo
Nov 21 '18 at 12:12





eventually X <- replicate(18, sample(runif(500), repl=TRUE)) is equivalent to your code.

– jogo
Nov 21 '18 at 12:12













Thanks Yogo, the actual numbers in U are not uniformly distributed, but drawn by a more complex function (copula). I will edit your suggestion as the solution, thank you very much

– Ozaki_san
Nov 21 '18 at 12:16





Thanks Yogo, the actual numbers in U are not uniformly distributed, but drawn by a more complex function (copula). I will edit your suggestion as the solution, thank you very much

– Ozaki_san
Nov 21 '18 at 12:16




1




1





I suggest to remove the answer from the question. Then please accept the answer which @jogo provided in order to indicate that you have found an acceptable solution.

– Heikki
Nov 21 '18 at 12:37





I suggest to remove the answer from the question. Then please accept the answer which @jogo provided in order to indicate that you have found an acceptable solution.

– Heikki
Nov 21 '18 at 12:37












1 Answer
1






active

oldest

votes


















2














You can speed up by calculating column by column:



for (i in 1:18) X[, i] <- Marginals[U[, i], i]


Eventually the following is equivalent to your code:



X <- replicate(18, sample(runif(500), repl=TRUE))


(this will not be much faster than my first variant, but the code is more compact)






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411021%2fmost-efficient-way-to-use-values-of-matrix-as-row-indices-to-lookup-values-in-an%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You can speed up by calculating column by column:



    for (i in 1:18) X[, i] <- Marginals[U[, i], i]


    Eventually the following is equivalent to your code:



    X <- replicate(18, sample(runif(500), repl=TRUE))


    (this will not be much faster than my first variant, but the code is more compact)






    share|improve this answer






























      2














      You can speed up by calculating column by column:



      for (i in 1:18) X[, i] <- Marginals[U[, i], i]


      Eventually the following is equivalent to your code:



      X <- replicate(18, sample(runif(500), repl=TRUE))


      (this will not be much faster than my first variant, but the code is more compact)






      share|improve this answer




























        2












        2








        2







        You can speed up by calculating column by column:



        for (i in 1:18) X[, i] <- Marginals[U[, i], i]


        Eventually the following is equivalent to your code:



        X <- replicate(18, sample(runif(500), repl=TRUE))


        (this will not be much faster than my first variant, but the code is more compact)






        share|improve this answer















        You can speed up by calculating column by column:



        for (i in 1:18) X[, i] <- Marginals[U[, i], i]


        Eventually the following is equivalent to your code:



        X <- replicate(18, sample(runif(500), repl=TRUE))


        (this will not be much faster than my first variant, but the code is more compact)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 '18 at 12:33

























        answered Nov 21 '18 at 12:23









        jogojogo

        10k92135




        10k92135
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411021%2fmost-efficient-way-to-use-values-of-matrix-as-row-indices-to-lookup-values-in-an%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            Npm cannot find a required file even through it is in the searched directory