R中用嵌套的for循环来评估矩阵周围的单元格

3
我有一个7x7的矩阵:
Mat<-matrix(nrow=7,ncol=7)

带有特定的元素:

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

我正在生成随机元素,并希望测试它们是否加起来等于指定的值。
我已经编写了以下代码:
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]))

现在我正在尝试使用嵌套的for循环使其更加高效:

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]))
}
}

问题在于向量元素将被覆盖,再添加另一个 for 循环没有任何意义。 我还有一个问题,即如何在发现一个false时重新运行模拟。

如果您分配一个新对象并填充它的新值,它就不会被覆盖。此外,请查看apply。另外,solve是一个函数。您正在将其用作矩阵名称? - Rich Scriven
@RichardScriven,虽然问题的表述不是很好,但我认为问题本身还是挺有意思的。由于存在跨行/列依赖关系,因此apply函数显然无法解决这个问题(如果可以解决,我很想知道如何解决)。 - BrodieG
1
@BrodieG 和 OP,如果我的话让你们有所误解,请接受我的道歉。我只是想提出一个新的变量来存储新的 TF 值,并且对于你们使用 solve 的方式感到困惑。这是一个非常有趣的问题。 - Rich Scriven
@RichardScriven,就我而言没有问题。 - BrodieG
是的,我把矩阵的名称命名为“solve”。谢谢提醒,我会更改的。 如果你们感兴趣的话,我可以随时向你们更新进展情况,或者提供代码的问题和答案。我已经编写了代码,但我正在优化它以使其更加高效。 - sempedocles
1个回答

1

让我们首先回答以下问题:

如何计算矩阵中每个单元格周围单元格的总和?

据我所知,这实际上并不是一件简单的事情(好奇是否有人能想出更酷的方法)。这里有一个潜在的解决方案,虽然远远不够简洁。让我们先看看函数的结果。在这里,我们将创建仅包含1的矩阵,以便检查结果是否合理(角落应该加起来为3,因为只有三个相邻的单元格,内部为8等):

> 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

这个有效!

现在,让我们回答你实际的问题:

compute_neighb_sum(mx) == mx

这应该对所有等于其周围数字之和的单元格返回TRUE。让我们确认一下:

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

果然,我们得到了预期的坐标:

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

现在,这是函数:

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
  )  
}

非常感谢@BordieG提供了非常好的答案。 我不知道它是否完全适用于我的问题,但我会尝试调整代码。 - sempedocles

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接