all combinations of k numbers between 0 and n whose sum equals n, speed optimization

后端 未结 5 1854
Happy的楠姐
Happy的楠姐 2021-01-25 18:42

I have this R function to generate a matrix of all combinations of k numbers between 0 and n whose sum equals n. This is one of the bottlenecks of my program as it becomes extre

5条回答
  •  一整个雨季
    2021-01-25 19:15

    The following can be done with lapply.

    ls1 <- list()
    for(i in 1:k) {
      ls1[[i]] <- 0:n
    }
    

    Try substituting this is and see if you get any speed up.

    ls1 = lapply(1:k,function(x) 0:n)
    

    I changed 'ls' to 'ls1' because ls() is an R function.

提交回复
热议问题