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.
4 Answers
2
votes
1
votes
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.
?hclust
. You may define a distance between each couple of rows (column) of your array saya
andb
just by computingabs(a-b)
. Thenhclust()
will return you a good permutation that puts every row (and column) close to ones having non-zero values on the same indices – Alipheatmap()
of packagepheatmap
. It will provide you a visualization of your block-diagonal matrix. – Ali