How can I create a sparse matrix containing (3,3) block matrices on the main diagonal and on diagonals below and above the main diagonal without using loops?
Show older comments
The idea is to create a (3m,3m) sparse matrix with small (3,3) matrices on the main diagonal and on diagonals below and above the main diagonal. By saying this, I want that each time the main diagonal of the small (3,3) matrix is on the main diagonal or on another diagonal respectively. There is no gap between the small (3,3) matrices on the diagonals. How can I use the functions sparse, spdiags, blkdiag to create this matrix? If there are other functions guaranteeing sparsity that's fine.
Accepted Answer
More Answers (1)
Iain
on 20 May 2013
0 votes
You can initialise a sparse matrix as:
matrix = sparse(zeros(3*m,3*m));
If you then use it as:
matrix(1:3,1:3) = [a b c; d e f; g h i];
matrix(3+1:3,3+1:3) = [j k l; m n o; p q r];
... etc you will get what I think you're asking for.
Categories
Find more on Sparse 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!