I have an 8x8 matrix:
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
Code to create it:
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)
The ones have been extracted as coordinates:
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
I need a simple, vectorized approach to clustering the ones into groups of adjacent coordinates. (For the purposes of this task, I would like 4-way up/down/left/right adjacency, but you may also want the option of 8-way adjacency including the diagonals.)
In the example, we would end up with 5 clusters of cells:
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
The 4-way adjacency check is pretty simple: sum(abs(onecoords[1,] - onecoords[2,])) == 1
But I'm struggling to work out how to vectorize this efficiently.