[R] For loop

Joshua Wiley jwiley.psych at gmail.com
Sun Oct 31 23:01:06 CET 2010


Actually, I'm able to gain another second by limiting -/+s in the
subscripts (maybe just an artifact?  I ran it a couple of times to
check, but still).


system.time(for(k in 22:2) {
  tmp <- xx.2 * v.2[, k]
 for(j in 20000:1) {
  vv.2[j, k] <- min(tmp[j], vv.2[j+1, k])
 }
 v.2[-1, k-1] <- h.2[1, k-1] + vv.2[-20001, k]
 vv.2[20001, k-1] <- v.2[20001, k-1]
})


On Sun, Oct 31, 2010 at 2:39 PM, Joshua Wiley <jwiley.psych at gmail.com> wrote:
> Hi,
>
> Using the code below I got almost a 60% speedup.  Obviously this is
> largely dependent on there being relatively fewer columns than rows.
>
> HTH,
>
> Josh
>
>
> #############################
> ## Create data
> h <- structure(c(0.25, 0.25, 0, 0, 0, 0, -0.25, -0.25, -0.25, -0.25,
> -0.5, -0.5, 0, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
> 0.25), .Dim = c(1L, 22L), .Dimnames = list(NULL, NULL))
> xx <- seq(0, 1, 0.00005)
> v <- matrix(nrow=20001,ncol=22)
> vv <- matrix(nrow=20001,ncol=22)
> ## Create duplicates for comparison
> h.2 <- h; xx.2 <- xx; v.2 <- v; vv.2 <- vv
>
> ## Old
> system.time(for (y in 1:20001) {v[y, 22] <- h[1, 22]})
> ## New
> system.time(v.2[, 22] <- h.2[1, 22])
>
> ## no speedup possible here
> vv[20001,22] <- v[20001,22]
> vv.2[20001,22] <- v.2[20001,22]
>
>
> ## Old
> system.time(
> for(k in 21:1) {
>  for(j in 20001:2) {
>  vv[j-1, k + 1] <- min(xx[j-1] * v[j-1,k+1], vv[j,k+1])
>  v[j, k] <- h[1, k] + vv[j-1, k+1]
>  }
> vv[20001, k] <- v[20001, k]
> })
> #   user  system elapsed
> # 20.733   0.044  25.869
>
> ## New
> system.time(for(k in 21:1) {
>  tmp <- xx.2 * v.2[, k+1]
>  for(j in 20001:2) {
>  vv.2[j-1, k + 1] <- min(tmp[j-1], vv.2[j, k+1])
>  }
>  v.2[-1, k] <- h.2[1, k] + vv.2[-20001, k+1]
>  vv.2[20001, k] <- v.2[20001, k]
> })
> #   user  system elapsed
> #  9.184   0.012  10.500
>
> ## Check that new is identical to old
> identical(vv, vv.2)
> identical(v, v.2)
>
> On Sun, Oct 31, 2010 at 3:58 AM, Astabenefica <astabenefica at hotmail.it> wrote:
>> Thanks for the suggestions.
>> I add some more detail to clarify:
>>
>> h=matrix(nrow=1,ncol=22)
>>
>> In my data h is:
>> (0.25  0.25  0  0  0  0  -0.25  -0.25  -0.25  -0.25  -0.5  -0.5  0  0.25  0.25  0.25  0.25  0.25  0.25  0.25  0.25  0.25)
>>
>>
>>
>> xx<-seq(0,1,0.00005)
>>
>> v=matrix(nrow=20001,ncol=22)
>> vv=matrix(nrow=20001,ncol=22)
>> for (y in 1:20001) {
>> v[y,22]=h[1,22]
>> }
>> vv[20001,22]=v[20001,22]
>> for(k in 21:1) {
>>  for(j in 20001:2) {
>>   vv[j-1,k+1]=min(xx[j-1]*v[j-1,k+1],vv[j,k+1])
>>   v[j,k]=h[1,k]+vv[j-1,k+1]
>>  }
>> vv[20001,k]=v[20001,k]
>> }
>>
>>
>>
>> The idea of using Rcpp seems to be good. Having never used this package will give a look.
>> Somewhere I read an example like this:
>>
>>
>> for (i in 1:R) {
>>  res[i]<-f()
>>  NULL
>> }
>>
>> where f() is a function, but I don't how can I trasform my code:
>> vv[j-1,k+1]=min(xx[j-1]*v[j-1,k+1],vv[j,k+1])
>> v[j,k]=h[1,k]+vv[j-1,k+1]
>> in a function
>>
>>
>>
>>
>>
>>
>>
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at 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.

-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list