Mike White
2006-Nov-29 13:49 UTC
[R] Block clustering and largest square block in binary matrix
Hi I have a large binary square matrix (about 2500 x 2500) obtained from a distance matrix on x (smaller simulation below), in which I want to find the largest square block of 1's for all permutations of the rows and columns. I then want to remove the rows and columns corresponding to this block and repeat the process, so that finally all the rows of data in x are assigned to a particular square block or are isolates. Does anyone know of a function or algorithm that can do this? I have looked at the blockmodel function in the sna package but this does not appear to find the largest block and then the next largest block etc. I have also search the email archives without success. rm(list=ls()) set.seed(1) x<-rnorm(100,10,1) d.mat<-as.matrix(dist(x)) d.mat[d.mat<=0.2]<- -1 d.mat[d.mat>0.2]<-0 d.mat<-d.mat*-1 ## I have tried sorting by row and col sums does not identify the largest squate block in every situation rs<-rowSums(d.mat) cs<-colSums(d.mat) d.mat1<-d.mat[order(rs), order(cs)] Any help would be much appreciated Thanks Mike White