#define a simple binary matrix
tmat = { matrix(c( 0,0,0,1,0,0,1,1,0,1,
0,0,1,0,1,0,0,0,0,0,
0,1,NA,1,0,1,0,0,0,1,
1,0,1,1,1,0,1,0,0,1,
0,1,0,1,0,1,0,0,0,1,
0,0,1,0,1,0,0,1,1,0,
1,0,0,1,0,0,1,0,0,1,
0,1,0,0,0,1,0,0,0,1,
0,0,1,1,1,0,0,0,0,1,
1,1,1,0,0,0,0,0,0,1),nr=10,byrow=TRUE) }
#do the connected component labelling
ccl.mat = ConnCompLabel(tmat)
ccl.mat
image(t(ccl.mat[10:1,]),col=c('grey',rainbow(length(unique(ccl.mat))-1)))
Run the code above in your browser using DataLab