Hi there,

In my code, there is a for loop like the following:

   pmatrix <- matrix(NA, nrow = 99, ncol = 10000)
   qmatrix <- matrix(NA, nrow = 99, ncol = 3)
   paf <- seq(0.01, 0.99, 0.01)
   for (i in 1:10000) {
       p.r.1 <- rnorm(1000, 1, 0.5)
       p.r.2 <- rnorm(1000, 2, 1.5)
       p.r.3 <- rnorm(1000, 3, 1)
       pmatrix[,i] <- quantile(c(p.r.1, p.r.2, p.r.3), paf)
   }
   for (i in 1:99) {
      qmatrix[i,] <- quantile(pmatrix[i,], c(0.05, 0.5, 0.95))
   }

Because of the number of loop is very large, e.g., 10000 here, the code is very slow.

Is it possible to optimize the code?

Any suggestion will be greatly appreciated.

Regards,
Jinsong

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to