Hi All, I wonder if I can avoid the for() loop in any of the following loops.These loops are a part of a larger code which I'm trying to accelerate. n=6 m=4 x<-c(0,1,1) 1st loop for (i in 1:m-1) { d[i]<- n- (sum(x[(1):(i)])) - i } e<- n*(prod(d)) 2nd loop LD<-list() for (i in 1:(m-1)) { LD[[i]]<-seq(0,x[i],1) } LD[[m]]<-seq(0,(n-m-sum(x)),1) LED<-expand.grid (LD) LED<-as.matrix(LED) 3rd loop for (i in 1:(m-1)) { h[i]<- choose(x[i],LED[j,i]) } 4th loop for (i in 1:(m-1)) { lm[i]<-(sum(LED[j,1:i])) + i } I appreciate if anyone has any suggestions or references. Thanks in advance. Maram Salem [[alternative HTML version deleted]]
If this code is slow it is not because you are using loops, but because you are dynamically building your vectors and lists and their size needs to change with each iteration causing significant unnecessary computational overhead. If you simply do something like d <- numeric(m-1) for (i in 1:m-1) { d[i] <- n - sum(x[1:i]) - i } for all of your loops, you will see already see very significant speedup. (If you look at my code formatting, and compare it with your own you may also benefit.) The bottom line: the point is not to avoid for-loops, but to speed up your code. Nb. if you want to avoid loops for some aesthetic reason, read about apply() and its siblings, and experiment with it. Of course, internally an apply() statement uses loops... NNb: Do you know how to profile your code? How do you know which part of your code is actually slowing it down? B. On Oct 25, 2015, at 6:42 AM, Maram SAlem <marammagdysalem at gmail.com> wrote:> Hi All, > > I wonder if I can avoid the for() loop in any of the following loops.These > loops are a part of a larger code which I'm trying to accelerate. > > n=6 > m=4 > x<-c(0,1,1) > > 1st loop > > for (i in 1:m-1) > { > d[i]<- n- (sum(x[(1):(i)])) - i > } > e<- n*(prod(d)) > > > 2nd loop > > LD<-list() > for (i in 1:(m-1)) > { > LD[[i]]<-seq(0,x[i],1) > } > > LD[[m]]<-seq(0,(n-m-sum(x)),1) > LED<-expand.grid (LD) > LED<-as.matrix(LED) > > > 3rd loop > > for (i in 1:(m-1)) > > { > > h[i]<- choose(x[i],LED[j,i]) > > } > > > > 4th loop > > > for (i in 1:(m-1)) > > { > > lm[i]<-(sum(LED[j,1:i])) + i > > } > > > I appreciate if anyone has any suggestions or references. > > > Thanks in advance. > > > Maram Salem > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
> On 25 Oct 2015, at 11:42, Maram SAlem <marammagdysalem at gmail.com> wrote: > > Hi All, > > I wonder if I can avoid the for() loop in any of the following loops.These > loops are a part of a larger code which I'm trying to accelerate. > > n=6 > m=4 > x<-c(0,1,1) > > 1st loop > > for (i in 1:m-1) > { > d[i]<- n- (sum(x[(1):(i)])) - i > } > e<- n*(prod(d)) > >On the basis of the other loops I presume you mean for( in in 1:(m-1)) in stead of what you wrote. You get the same result with d <- n - cumsum(x) - (1:(m-1)) Berend> 2nd loop > > LD<-list() > for (i in 1:(m-1)) > { > LD[[i]]<-seq(0,x[i],1) > } > > LD[[m]]<-seq(0,(n-m-sum(x)),1) > LED<-expand.grid (LD) > LED<-as.matrix(LED) > > > 3rd loop > > for (i in 1:(m-1)) > > { > > h[i]<- choose(x[i],LED[j,i]) > > } > > > > 4th loop > > > for (i in 1:(m-1)) > > { > > lm[i]<-(sum(LED[j,1:i])) + i > > } > > > I appreciate if anyone has any suggestions or references. > > > Thanks in advance. > > > Maram Salem > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
Thanks a lot Boris and Berend. I'll consider the brackets ((m-1) in every loop). In addition, I'll read more on profiling my code. In fact,I'm using the apply () in another part of my code. Thanks again for helping. Maram Salem On 25 October 2015 at 14:26, Berend Hasselman <bhh at xs4all.nl> wrote:> > > On 25 Oct 2015, at 11:42, Maram SAlem <marammagdysalem at gmail.com> wrote: > > > > Hi All, > > > > I wonder if I can avoid the for() loop in any of the following > loops.These > > loops are a part of a larger code which I'm trying to accelerate. > > > > n=6 > > m=4 > > x<-c(0,1,1) > > > > 1st loop > > > > for (i in 1:m-1) > > { > > d[i]<- n- (sum(x[(1):(i)])) - i > > } > > e<- n*(prod(d)) > > > > > On the basis of the other loops I presume you mean > > for( in in 1:(m-1)) > > in stead of what you wrote. > > You get the same result with > > d <- n - cumsum(x) - (1:(m-1)) > > > Berend > > > > 2nd loop > > > > LD<-list() > > for (i in 1:(m-1)) > > { > > LD[[i]]<-seq(0,x[i],1) > > } > > > > LD[[m]]<-seq(0,(n-m-sum(x)),1) > > LED<-expand.grid (LD) > > LED<-as.matrix(LED) > > > > > > 3rd loop > > > > for (i in 1:(m-1)) > > > > { > > > > h[i]<- choose(x[i],LED[j,i]) > > > > } > > > > > > > > 4th loop > > > > > > for (i in 1:(m-1)) > > > > { > > > > lm[i]<-(sum(LED[j,1:i])) + i > > > > } > > > > > > I appreciate if anyone has any suggestions or references. > > > > > > Thanks in advance. > > > > > > Maram Salem > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at 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]]