[R-sig-Geo] Optimising raster/matrix algrebra

Jonathan j2kennel at gmail.com
Mon Nov 12 16:05:40 CET 2012


Hello Alastair,

In my experience when I've been up against speed issues I switch to do most
of my computation in arrays or matrices and then convert back to a raster at
the end.  The code isn't as elegant as using the raster functions but you
can pick up some speed.  If you need the xy stack for other uses you will
need to adjust the solution.

# YOUR SOLUTION
x <- fun(100,100,1000)
y <- fun(100,100,1000)
system.time(xy <- x==1&y==1)
system.time(xy.tot <- sum(xy))

# A POSSIBLE SOLUTION WHICH SHOULD BE FASTER
x1 <- values(x)
y1 <- values(y)
system.time({xy1 <- x1==1 & y1==1})
xy.tot1 <- raster(ncol=100,nrow=100)
system.time(xy.tot1[] <- rowSums(xy1))

all.equal(xy.tot1,xy.tot)

-Jonathan




--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Optimising-raster-matrix-algrebra-tp7581668p7581672.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list