How to create sparse matrix with blkdiag ?
조회 수: 16 (최근 30일)
이전 댓글 표시
Hello guys,
I have a matrix "param" of size (N,3,3) with N being relatively large. I want to create a sparse 3*N*N square matrix whose diagonals are param(1,:,:) which are 3x3 matrix. Here is what I do:
paramC=num2cell(param,[2 3]);
DiagMatrix=blkdiag(paramC{:});
But, when I try to do that I have a memory problem when calling blkdiag, arguing that the matrix is too large (3*N*N square matrix). But the matrix should be really sparse as it is a tridiagonal matrix.
I have tried multiple way to make matlab understand that this is a sparse matrix include:
paramC=num2cell(sparse(param),[2 3]);
DiagMatrix=blkdiag(paramC{:});
paramC=num2cell(param,[2 3]);
DiagMatrix=blkdiag(sparse(paramC{:}));
but everytime I get the error:
Undefined function 'sparse' for input arguments of type 'double' and attributes 'full 3d real'.
Do you have a way to fix this issue ?
Thanks a lot !
댓글 수: 0
답변 (2개)
John D'Errico
2015년 12월 20일
편집: John D'Errico
2015년 12월 20일
A sparse tridiagonal matrix, built directly from the diagonal elements is one of the things that my blktridiag can build. It is on the file exchange.
It is a bit unclear if your goal is to build a block diagonal matrix or a tridiagonal matrix, or exactly what. For example, you can use spdiags to build a sparse tridiagonal matrix. And you CAN use blkdiag to build a sparse block diagonal matrix. You need to force each block to be itself sparse.
C = {sparse(rand(3)),sparse(rand(3)),sparse(rand(3))};
A = blkdiag(C{:});
whos A
Name Size Bytes Class Attributes
A 9x9 512 double sparse
spy(A)
댓글 수: 0
Walter Roberson
2015년 12월 20일
편집: Walter Roberson
2015년 12월 20일
You face the fundamental problem that in MATLAB, only 2D sparse matrices are supported, not 3D.
"accumarray groups data into bins using n-dimensional subscripts, but sparse groups data into bins using 2-D subscripts."
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!