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@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.