简体   繁体   English

查找矩阵 (R) 中相邻值的数量

[英]Find number of neighbouring values in a matrice (R)

My matrice is like this:我的矩阵是这样的:

  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
 [1,]    0    0    0    0    0    0    0    0    0
 [2,]    0    0    0    0    0    0    0    0    0
 [3,]    0    0    0    0    0    0    0    0    0
 [4,]    0    0    0    0    0    0    0    1    1
 [5,]    0    0    0    0    0    0    1    1    0
 [6,]    0    0    0    0    0    1    0    0    0
 [7,]    0    0    0    0    1    1    0    0    0
 [8,]    0    0    0    0    1    0    0    0    0
 [9,]    0    0    0    0    1    0    0    0    0
[10,]    0    0    0    0    1    1    0    0    0
[11,]    0    0    0    0    0    1    0    0    0
[12,]    0    0    0    0    0    1    1    1    1
[13,]    0    0    0    0    0    0    0    0    0
[14,]    0    0    0    0    0    0    0    0    0
[15,]    0    0    0    0    0    0    0    0    0
[16,]    0    0    0    0    0    0    0    0    0
[17,]    0    0    0    0    0    0    0    0    0
      [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17]
 [1,]     0     0     0     0     0     0     0     0
 [2,]     0     0     0     0     0     0     0     0
 [3,]     0     0     0     0     0     0     0     0
 [4,]     1     1     0     0     0     0     0     0
 [5,]     0     1     0     0     0     0     0     0
 [6,]     0     1     0     0     0     0     0     0
 [7,]     0     1     0     0     0     0     0     0
 [8,]     0     1     0     0     0     1     0     0
 [9,]     0     1     0     0     0     1     0     0
[10,]     1     1     0     0     0     1     0     0
[11,]     1     1     0     1     1     0     0     0
[12,]     1     1     1     1     0     0     0     0
[13,]     0     0     0     0     0     0     0     0
[14,]     0     0     0     0     0     0     0     0
[15,]     0     0     0     0     0     0     0     0
[16,]     0     0     0     0     0     0     0     0
[17,]     0     0     0     0     0     0     0     0
      [,18]
 [1,]     0
 [2,]     0
 [3,]     0
 [4,]     0
 [5,]     0
 [6,]     0
 [7,]     0
 [8,]     0
 [9,]     0
[10,]     0
[11,]     0
[12,]     0
[13,]     0
[14,]     0
[15,]     0
[16,]     0
[17,]     0

How can I find the number of values which has 1 in neighbour?我怎样才能找到邻居中有 1 的值的数量? (neighbour of a pixel is the value above the value, below the value, to the right, to the left, top right, top left, below left, below right). (像素的邻居是值上方、值下方、向右、向左、右上、左上、左下方、右下方的值)。

I just need to get a way of devising how I can even find the number of values which has 1 above/below it.我只需要找到一种方法来设计我什至可以找到上面/下面有 1 的值的数量。 If I get that, I'll be able to solve the other variables of the problem (top right and such).如果我明白了,我将能够解决问题的其他变量(右上角等)。

I've been experimenting around with which such as which(imageMatrix == 1, arr.ind = TRUE)[1,1] .我一直在试验which which(imageMatrix == 1, arr.ind = TRUE)[1,1] But I cannot figure it out.但我想不通。 (ImageMatrix is the name of the matrix) (ImageMatrix为矩阵名称)

Can anyone lend me a hand on how I can begin with the problem so I get a jump?任何人都可以帮助我了解如何从这个问题开始,以便我能有所作为吗?

In the following we use the example matrix m generated reproducibly in the Note at the end.在下文中,我们使用在最后的注释中可重复生成的示例矩阵 m。

1) Append a row and column of zeros on each end and then apply rollsum, transpose, apply it again and transpose again. 1) Append 每一端的一行和一列零,然后应用 rollsum,转置,再次应用并再次转置。 Finally subtract the original matrix so that only neighbors are counted.最后减去原始矩阵,这样只计算邻居。 This solution is the most compact of those here.这个解决方案是这里最紧凑的解决方案。

library(zoo)

m2 <- rbind(0, cbind(0, m, 0), 0)
t(rollsum(t(rollsum(m2, 3)), 3)) - m
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    2    3    5    3    2
## [2,]    3    5    6    4    2
## [3,]    2    6    5    5    2
## [4,]    2    5    2    2    1
## [5,]    1    3    2    2    1

2) A second approach is the following. 2)第二种方法如下。 It would be much faster than the others here.它会比这里的其他人快得多。

nr <- nrow(m)
nc <- ncol(m)

mm <- cbind(m[, -1], 0) + m + cbind(0, m[, -nc]) 
rbind(mm[-1, ], 0) + mm + rbind(0, mm[-nr, ]) - m
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    2    3    5    3    2
## [2,]    3    5    6    4    2
## [3,]    2    6    5    5    2
## [4,]    2    5    2    2    1
## [5,]    1    3    2    2    1

3) Using loops we can write the following. 3)使用循环我们可以写出以下内容。 It is probably the most straight forward.这可能是最直接的。 Note that subscripting by 0 omits that element and i %% 6 equals i if i is in 1, 2, 3, 4, 5 and equals 0 if i equals 0 or 6.请注意,下标为 0 会省略该元素,如果 i 在 1、2、3、4、5 中,则 i %% 6 等于 i,如果 i 等于 0 或 6,则等于 0。

nr <- nrow(m); nr1 <- nr + 1
nc <- ncol(m); nc1 <- nc + 1

mm <- 0 * m  # initialize result
for(i in seq_len(nr)) 
  for(j in seq_len(nc)) 
    mm[i, j] <- sum(m[seq(i-1, i+1) %% nr1, seq(j-1, j+1) %% nc1]) - m[i,j]
mm
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    2    3    5    3    2
## [2,]    3    5    6    4    2
## [3,]    2    6    5    5    2
## [4,]    2    5    2    2    1
## [5,]    1    3    2    2    1

Note笔记

set.seed(123)
m <- +matrix(rnorm(25) > 0, 5)

m
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    0    1    1    1    0
## [2,]    0    1    1    1    0
## [3,]    1    0    1    0    0
## [4,]    1    0    1    1    0
## [5,]    1    0    0    0    0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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