Thanks a lot for helping William. Will check the reference as well. Sent from my iPhone
> On Oct 15, 2015, at 5:52 PM, William Dunlap <wdun...@tibco.com> wrote: > > Doing enumerative combinatorics with rejection methods rarely > works well. Try mapping your problem to the problem of choosing > m-1 items from n-1. E.g., your code was > f0 <- function(n, m) { > stopifnot(n > m) > D<-matrix(0,nrow=n-m+1,ncol=m-1) > for (i in 1:m-1){ > D[,i]<-seq(0,n-m,1) > } > ED <- do.call(`expand.grid`,as.data.frame(D)) > ED<-unname(as.matrix(ED)) > lk<-which(rowSums(ED)<=(n-m)) > ED[lk,] > } > and I think the following does the same thing in much less space by > transforming the output of combn(). > f1 <- function(n, m) { > stopifnot(n > m) > r0 <- t(diff(combn(n-1, m-1)) - 1L) > r1 <- rep(seq(from=0, len=n-m+1), choose( seq(to=m-2, by=-1, len=n-m+1), > m-2)) > cbind(r0[, ncol(r0):1, drop=FALSE], r1, deparse.level=0) > } > The code for adding the last column is a bit clumsy and could probably be > improved. Both f0 and f1 could also be cleaned up to work for m<=2. > > See Feller vol. 1 or Benjamin's "Proofs that (really) count" for more on this > sort of thing. > > > > Bill Dunlap > TIBCO Software > wdunlap tibco.com > >> On Thu, Oct 15, 2015 at 7:45 AM, Maram SAlem <marammagdysa...@gmail.com> >> wrote: >> Dear All, >> >> I'm trying to do a simple task (which is in fact a tiny part of a larger >> code). >> >> I want to create a matrix, D, each of its columns is a sequence from 0 to >> (n-m), by 1. Then, using D, I want to create another matrix ED, whose rows >> represent all the possible combinations of the elements of the columns of >> D. Then from ED, I'll select only the rows whose sum is less than or equal >> to (n-m), which will be called the matrix s. I used the following code: >> >> > n=5 >> > m=3 >> > D<-matrix(0,nrow=n-m+1,ncol=m-1) >> > for (i in 1:m-1) >> + { >> + D[,i]<-seq(0,n-m,1) >> + } >> > ED <- do.call(`expand.grid`,as.data.frame(D)) >> > ED<-as.matrix(ED) >> >> > lk<-which(rowSums(ED)<=(n-m)) >> >> > s<-ED[lk,] >> >> >> This works perfectly well. But for rather larger values of n and m (which >> are not so large actually), the number of all possible combinations of the >> columns of D gets extremely large giving me this error (for n=25, m=15): >> >> > ED <- do.call(`expand.grid`,as.data.frame(D)) >> Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : >> invalid 'times' value >> In addition: Warning message: >> In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : >> NAs introduced by coercion to integer range >> >> >> Any help or suggestions will be greatly appreciated. >> >> Thanks, >> >> Maram Salem >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.