extract diagonal blocks of sparse matrix

2 views (last 30 days)
Hi everybody,
I have a large sparse n by n matrix A, and also a index vector idxVec. Elements in idxVec like 2,3, 5, 1, etc, and sum(idxVec) =n. How can I get the diagonal blocks specified in idexVec. i.e., block diagonal matrix like blkdiag(A(1:2,1:2), A(3:5,3:5), A(6:10,6:10), A(11:11, 11:11), ...). where the index is get acording to indVec.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 26 May 2012
eg
A = randi(1548,11);
k = mat2cell(1:size(A,1),1,[2 3 5 1]);
t = cellfun(@(x)A(x,x),k,'un',0);
out = blkdiag(t{:});

More Answers (0)

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!