[R] sampling quadrats of increasing size in a list of matrices
Chris Stubben
stubben at lanl.gov
Tue Jun 30 20:15:55 CEST 2009
Jens Oldeland wrote:
>
> I am looking for a solution on how to sample increasing sizes of
> "quadrats" in a list of matrices. Each matrix is a binary raster map and
> I want to know if there is a 1 or only 0 s in the sampling unit
> (quadrat, e.g. 4x4 cells).
>
You could just use a few loops to index the submatrices/quadrats, maybe
something like this.
set.seed(7)
x<-matrix(sample(c(0,1), 256, replace=TRUE, prob=c(.8,.2)), nrow=16,
byrow=TRUE)
# matrix size (16)
n<-nrow(x)
#quadrat sizes (1 2 4 8 16)
q<- 2^(0:sqrt(n))
# store results in list
quads<-vector("list", length(q) )
names(quads)<-q
for (i in 1:length(q) )
{
z<-c()
for(j in 1:(n-q[i]+1) )
{
for(k in 1:(n-q[i]+1))
{
y<-x[j:(j+q[i]-1), k:(k+q[i]-1)]
#do something with submatrix
z<-c(z, ifelse( sum(y)>0, 1,0))
#z<-c(z, sum(y) )
}
}
quads[[i]]<-z
}
#empty quads
sapply(quads, function(x) sum(x==0))
1 2 4 8 16
206 91 3 0 0
# total number of quadrats sampled
sapply(quads, length)
#or
(n-q+1)^2
For random sampling, try replacing 1:(n-q[i]+1) with sample( 1:(n-q[i]+1),
replace=TRUE) ?
Also, if you have a list of matrices, then maybe combine the code above into
a function and then apply it to your list using lapply.
Chris Stubben
--
View this message in context: http://www.nabble.com/sampling-quadrats-of-increasing-size-in-a-list-of-matrices-tp24270434p24276950.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list