我有一个8x8矩阵:

1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1

创建它的代码:
examplemat <- matrix(c(1, 1, rep(0, 6), 1, rep(0, 9), 1, 1, rep(0, 17), 1, rep(0, 7), 1, rep(0, 5), 1, rep(0, 11), 1), 8, 8, byrow=T)

它们被提取为坐标:
onecoords <- which(examplemat == 1, arr.ind=T)

      row col
 [1,]   1   1
 [2,]   2   1
 [3,]   1   2
 [4,]   3   3
 [5,]   3   4
 [6,]   7   4
 [7,]   5   6
 [8,]   6   6
 [9,]   8   8

我需要一个简单的,矢量化的方法来聚类成一组相邻的坐标。(在本任务中,我希望使用4向上/下/左/右邻接,但您也可能需要8向邻接(包括对角线)选项。)
在本例中,我们将得到5组单元:
      row col clus
 [1,]   1   1    A
 [2,]   2   1    A
 [3,]   1   2    A
 [4,]   3   3    B
 [5,]   3   4    B
 [6,]   7   4    C
 [7,]   5   6    D
 [8,]   6   6    D
 [9,]   8   8    E

四向邻接检查非常简单:sum(abs(onecoords[1,] - onecoords[2,])) == 1但我正在努力研究如何有效地将其矢量化。

最佳答案

感谢阿列克西斯拉兹和Carl Witthoft的非常有用的意见,让我自己回答这个问题。
四向邻接
要获得基于四向邻接的簇,请在曼哈顿距离上使用具有单个链接的分层簇:

cbind(onecoords, clus = cutree(hclust(dist(onecoords, "manhattan"), "single"), h = 1))
      row col clus
 [1,]   1   1    1
 [2,]   2   1    1
 [3,]   1   2    1
 [4,]   3   3    2
 [5,]   3   4    2
 [6,]   7   4    3
 [7,]   5   6    4
 [8,]   6   6    4
 [9,]   8   8    5

八向邻接
为了获得基于八路邻接(包括对角线)的簇,在最大距离上使用单链接的分层聚类:
cbind(onecoords, clus = cutree(hclust(dist(onecoords, "maximum"), "single"), h = 1))

曼哈顿与最大距离的区别
在示例的情况下,这些都给出相同的结果,但是如果您移除第一行,例如,您将看到曼哈顿距离产生6个簇,而最大距离产生5个簇。
cbind(onecoords[2:9,], clus = cutree(hclust(dist(onecoords[2:9,], "manhattan"), "single"), h = 1))
     row col clus
[1,]   2   1    1
[2,]   1   2    2
[3,]   3   3    3
[4,]   3   4    3
[5,]   7   4    4
[6,]   5   6    5
[7,]   6   6    5
[8,]   8   8    6
cbind(onecoords[2:9,], clus = cutree(hclust(dist(onecoords[2:9,], "maximum"), "single"), h = 1))
     row col clus
[1,]   2   1    1
[2,]   1   2    1
[3,]   3   3    2
[4,]   3   4    2
[5,]   7   4    3
[6,]   5   6    4
[7,]   6   6    4
[8,]   8   8    5

关于r - 标记矩阵中的相邻点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37945868/

10-12 19:11