Quantcast
Channel: R nested loop slow - Stack Overflow
Browsing latest articles
Browse All 4 View Live

Answer by Kevin Ushey for R nested loop slow

To write fast R code, you really need to re-think how you write functions. You want to operate on entire vectors, not just single observations at a time.If you're really deadset in writing C-style...

View Article



Answer by Ben Bolker for R nested loop slow

How about:at <- s_0 + t(apply(matrix(rnorm(samples*(steps+1),sd=sigma*sqrt(8e-4)), ncol=samples), 2, cumsum))(Haven't tested this carefully yet, but I think it should be right, and much faster.)

View Article

Answer by Jan for R nested loop slow

R can vectorize certain operations. In your case you can get rid of the outer loop by doing a following change.for(i in 2:(steps + 1)){ at[i,] = at[(i - 1),] + sigma * sqrt(.0008) *...

View Article

R nested loop slow

I have no idea why something like this should be slow:steps=500samples=100000s_0=2.1r=.02sigma=.2k=1.9at<-matrix(nrow=(steps+1),ncol=samples)at[1,]=s_0for(j in 1:samples){ for(i in 2:(steps+1)) {...

View Article
Browsing latest articles
Browse All 4 View Live




Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>