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?

6 次查看(过去 30 天)
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.
  2 个评论
Cedric
Cedric 2013-5-19
Did you generate these m 3x3 sparse matrices already or do you want to avoid building them and build directly the block diagonal large sparse matrix? If you have these small matrices already defined, what have you tried so far using BLKDIAG?
Manuel
Manuel 2013-5-20
I've got all the 3x3 matrices (they are not sparse) already. Basically they are the local Hessian operator on a manifold. Now I want to store them in this huge sparse matrix to do a Newton step (Newton Method). I've thought of using the Tensor Toolbox since blkdiag doesn't give me a sparse matrix.

请先登录,再进行评论。

采纳的回答

Cedric
Cedric 2013-5-20
BLKDIAG will give you a sparse matrix if one of its inputs is sparse. Try building the 3x3 matrices as sparse, or converting them (or just one) to sparse, before using BLKDIAG; it should work fine.

更多回答(1 个)

Iain
Iain 2013-5-20
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.
  1 个评论
Manuel
Manuel 2013-5-20
Thanks lain for your answer. You got my idea. But my matrix will be huge and with your idea I'll have to use a loop. But loops are way to slow for what I'm doing.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Sparse Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by