简体   繁体   中英

Nested for loop for evaluating surrounding cells of matrix in R

I have a 7x7 matrix:

Mat<-matrix(nrow=7,ncol=7)

With certain elements:

Mat[2,2]<-37
Mat[2,4]<-39
Mat[2,6]<-24
Mat[4,2]<-35
Mat[4,4]<-36
Mat[4,6]<-26
Mat[6,2]<-26
Mat[6,4]<-31
Mat[6,6]<-39

I am generating random elements and want to test if they add up to the specified values

I have written the following code:

TF<-c()
TF[1]<-isTRUE(Mat[2,2]==sum(Mat[1,1],Mat[1,2],Mat[1,3],Mat[2,1],Mat[2,3],Mat[3,1],Mat[3,2],Mat[3,3]))
TF[2]<-isTRUE(Mat[2,4]==sum(Mat[1,3],Mat[1,4],Mat[1,5],Mat[2,3],Mat[2,5],Mat[3,3],Mat[3,4],Mat[3,5]))
TF[3]<-isTRUE(Mat[2,6]==sum(Mat[1,5],Mat[1,6],Mat[1,7],Mat[2,5],Mat[2,7],Mat[3,5],Mat[3,6],Mat[3,7]))
TF[4]<-isTRUE(Mat[4,2]==sum(Mat[3,1],Mat[3,2],Mat[3,3],Mat[4,3],Mat[4,5],Mat[5,1],Mat[5,2],Mat[5,3]))
TF[5]<-isTRUE(Mat[4,4]==sum(Mat[3,3],Mat[3,4],Mat[3,5],Mat[4,3],Mat[4,5],Mat[5,3],Mat[5,4],Mat[5,5]))
TF[6]<-isTRUE(Mat[4,6]==sum(Mat[3,5],Mat[3,6],Mat[3,7],Mat[4,5],Mat[4,7],Mat[5,5],Mat[5,6],Mat[5,7]))
TF[7]<-isTRUE(Mat[6,2]==sum(Mat[5,1],Mat[5,2],Mat[5,3],Mat[6,1],Mat[6,3],Mat[7,1],Mat[7,2],Mat[7,3]))
TF[8]<-isTRUE(Mat[6,4]==sum(Mat[5,3],Mat[5,4],Mat[5,5],Mat[6,3],Mat[6,5],Mat[7,3],Mat[7,4],Mat[7,5]))
TF[9]<-isTRUE(Mat[6,6]==sum(Mat[5,5],Mat[5,6],Mat[5,7],Mat[6,5],Mat[6,7],Mat[7,5],Mat[7,6],Mat[7,7]))

Now i am trying to make it more efficient with a nested for loop:

O<-c(2,4,6)
for (G in O)
{
for (H in O)
{
TF[]<-isTRUE(Mat[G,H]==sum(Mat[G-1,H-1],Mat[G-1,H],Mat[G-1,H+1],Mat[G,H-1],Mat[G,H+1],Mat[G+1,H-1],Mat[G+1,H],Mat[G+1,H+1]))
}
}

The problem is that the vector element will be overwritten and it does not make any sense to add another for loop. I also have problem to find a way to rerun the simulation if one false is found.

Let's start first by answering the following question:

How do you compute the sum of every surrounding cell for each cell in a matrix?

This is actually not trivial as far as I can tell (curious to see if anyone else comes up with something cool). Here is a potential solution, though not even close to being succinct. Let's start by seeing the results of the function. Here we will create matrices of only 1 so we can check that the results make sense (corners should add to 3 since there are only three contiguous cells, insides to 8, etc.):

> compute_neighb_sum(matrix(1, nrow=3, ncol=3))
     [,1] [,2] [,3]
[1,]    3    5    3
[2,]    5    8    5
[3,]    3    5    3
> compute_neighb_sum(matrix(1, nrow=3, ncol=5))
     [,1] [,2] [,3] [,4] [,5]
[1,]    3    5    5    5    3
[2,]    5    8    8    8    5
[3,]    3    5    5    5    3
> compute_neighb_sum(matrix(1, nrow=7, ncol=7))
     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    3    5    5    5    5    5    3
[2,]    5    8    8    8    8    8    5
[3,]    5    8    8    8    8    8    5
[4,]    5    8    8    8    8    8    5
[5,]    5    8    8    8    8    8    5
[6,]    5    8    8    8    8    8    5
[7,]    3    5    5    5    5    5    3

This works!

Now, let's answer your actual question :

compute_neighb_sum(mx) == mx

and this should return TRUE for all cells that are equal to the sum of their surroundings. Lets confirm:

mx <- matrix(1, nrow=7, ncol=7)
mx[cbind(c(3, 6), c(3, 6))] <- 8   # make two interior cells equal two 8, which will be equal to sum of surroundings
which(compute_neighb_sum(mx) == mx, arr.ind=T) # you should look at `mx` to see what's going on

Sure enough, we get back the coordinates that we expect:

     row col
[1,]   3   3
[2,]   6   6

Now, here is the function:

compute_neighb_sum <- function(mx) {
  mx.ind <- cbind(        # create a 2 wide matrix of all possible indices in input
    rep(seq.int(nrow(mx)), ncol(mx)), 
    rep(seq.int(ncol(mx)), each=nrow(mx))
  )
  sum_neighb_each <- function(x) {
    near.ind <- cbind(         # for each x, y coord, create an index of all surrounding values
      rep(x[[1]] + -1:1, 3),
      rep(x[[2]] + -1:1, each=3)
    )
    near.ind.val <- near.ind[  # eliminate out of bound values, or the actual x,y coord itself
      !(
        near.ind[, 1] < 1 | near.ind[, 1] > nrow(mx)  |
        near.ind[, 2] < 1 | near.ind[, 2] > ncol(mx)  |
        (near.ind[, 1] == x[[1]] & near.ind[, 2] == x[[2]])
      ),
     ]
    sum(mx[near.ind.val])      # Now sum the surrounding cell values
  }
  `dim<-`(                     # this is just to return in same matrix format as input
    sapply(
      split(mx.ind, row(mx.ind)),   # For each x, y coordinate in input mx
      sum_neighb_each               # compute the neighbor sum
    ),
    c(nrow(mx), ncol(mx))      # dimensions of input
  )  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM