R or MATLAB: rearrange a large sparse matrix into a block diagonal matrix

I have a large sparse matrix, and I want to rearrange its rows or columns in order to turn the original matrix into a matrix of diagonal blocks. Does anyone know what functions in R or MATLAB can do this? Thank you very much.

+4
source share
4 answers

I am not set up to test this, but for the matrix m I would try:

 p = symrcm(m); block_m = m(p,p); 

If this does not work, check out the other functions listed in help sparfun to see if help helps you.

+2
source

The seriation package in R has a number of tools for the problems associated with this.

+1
source

Not quite sure if this is what you want, but in MATLAB this is what I used in the past. Probably not the most elegant solution. I go from sparse to full, and then grind the thing into square blocks.

 A=full(A); 

Then:

 blockedmatrix = mat2cell(A, (n*ones(1,size(A,1)/n)), ... (n*ones(1,size(A,1)/n))); %found somewhere on internetz 

This returns a cell where each record is of size nxn. It is easy to extract blocks of interest, manipulate them, and then restore them to a matrix using cell2mat.

+1
source

Maybe a little late in the game, but since there are teams available, here's a simple one. If you have matrix H and need a diagonal block shape, you can get it through the following lines ( MATLAB ):

 [p,q] = dmperm(H); H(p,q) 

which is equivalent to the Dulmazh-Mendelssohn permutation.

+1
source

All Articles