Nested loop for evaluating the surrounding matrix cells in R

I have a 7x7 matrix:

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

      

With some 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 items and want to check if they are added before the specified values

I wrote 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 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 there is no point in adding another loop. I also have a problem to find a way to repeat the simulation if a false one is found.

+3


source to share


1 answer


First, start by answering the following question:

How do you calculate the sum of each adjacent cell for each cell in the matrix?

It's not really trivial as far as I can tell (curious to see if anyone else comes along for something cool). Here's a potential solution, although it's not even close to being concise. Let's start by looking at the results of the function. Here we will create matrices of only 1 so that we can check that the results make sense (corners must be added to 3 since there are only three adjacent cells, interiors up 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

      

It works!

Now let's answer your real question :

compute_neighb_sum(mx) == mx

      



and this should return TRUE

for all cells that are equal to the sum of their environment. The confirmation:

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 going on

      

Of course, we return the coordinates that we expect:

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

      

Now, here's 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
  )  
}

      

+1


source







All Articles