Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a large sparse matrix, and I want to permute its rows or columns to turn the original matrix into a block diagonal matrix. Anyone knows which functions in R or MATLAB can do this? Thanks a lot.

like image 471
user1867277 Avatar asked Nov 30 '12 19:11

user1867277


3 Answers

I'm not really set up to test this, but for a matrix m I would try:

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

If that doesn't work, look through the other functions listed in help sparfun to see if any will help you out.

like image 183
Pursuit Avatar answered Nov 02 '22 15:11

Pursuit


The seriation package in R has a number of tools for problems related to this one.

like image 1
Glen_b Avatar answered Nov 02 '22 14:11

Glen_b


Not exactly sure if this is what you want, but in MATLAB this is what I have used in the past. Probably not the most elegant solution. I go from sparse to full and then chop 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 entry is of size nxn. It's easy to extract the blocks of interest, manipulate them, and then restore them to a matrix with cell2mat.

like image 1
johnish Avatar answered Nov 02 '22 15:11

johnish