0

I have a 200x200 matrix containing count information. I would like to randomly sample a 5 row x 5 column subset within my matrix and take the average of the counts within this subset. I want to do this 8 times randomly throughout my matrix (so maybe use a for-loop?) and then take the averages of these 8. I also need to use restrictions so that the random number generator doesn't sample at the very edge of my matrix.

I am new to R and programming in general, so a push in the right direction would be very helpful.

2
  • Is this sampling with replacements? Are the subsets allowed to overlap. Commented Jan 27, 2014 at 21:25
  • This is not sampling with replacements. Yes the subsets are allowed to overlap. I want to randomly sample a 5x5 portion of my matrix and find the average of that 5x5 portion. I want to do this 8 times and then take the average of the averages from the 5x5 portions. Sorry if this is confusing. And thank you for your help! Commented Jan 29, 2014 at 21:34

2 Answers 2

1

Here's an approach:

# an example 10x10 matrix
mat <- matrix(1:100, 10)
nc <- ncol(mat) # number of columns
nr <- nrow(mat) # number of rows

size <- 5 # size of the subset matrix
nmat <- 8 # number of submatrices

# sample indices of submatrices
set.seed(1)
idxc <- sample(seq(2, nc - size), size = nmat, replace = TRUE)
idxr <- sample(seq(2, nr - size), size = nmat, replace = TRUE)

# create a list of 8 submatrices
res <- mapply(function(x, y) mat[seq(x, x + size - 1), seq(y, y + size - 1)],
              idxr, idxc, SIMPLIFY = FALSE)

# calculate the average of the averages
mean(unlist(res))
# [1] 53.875
Sign up to request clarification or add additional context in comments.

3 Comments

The first part works great! Thank you! For the averaging though, I would like to find the average of the counts in the 5x5 subset (1 number). I would like to do this for all 8 different subsets. I would then like to find the average of these 8 subsets (instead of finding the average of the different cell values). This will give me 1 number at the end.
Is there a way to get the code to randomly sample different parts of the matrix every time I run it? Currently, it gives me the same answer each time I run it.
@MaryR Do not run set.seed(1). It resets the random number generator to a certain state. It is not necessary for the approach. I used it for reproducibility.
0

An alternative that assumes you're always pulling out a square. (ie, the rows/columns are right beside each other).

y <- matrix(round(rep(runif(200),200)*100),ncol=200,nrow=200)

sq <- seq(1,196,1) 
blocks <- sample(sq,8) # no replacements of squares

mean(
  sapply(blocks, 
    function(x) mean(y[x:(4+x),x:(4+x)])
  )
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.