Query regarding a sparse complex matrix
이전 댓글 표시
I have a complex matrix, I have to create its sparse form using sparse function. How do I store it in lesser bytes than the original complex matrix? In the first case it is storing in 144 bytes but in the sparse case it is storing in 152 bytes. Any example
A =
0.1620 + 0.8048i 0.3402 + 0.7727i 0.0000 + 0.0000i
0.2497 + 0.1178i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.3937 + 0.6184i 0.4527 + 0.2691i 0.0000 + 0.0000i
>> B = sparse(A)
B =
(1,1) 0.1620 + 0.8048i
(2,1) 0.2497 + 0.1178i
(3,1) 0.3937 + 0.6184i
(1,2) 0.3402 + 0.7727i
(3,2) 0.4527 + 0.2691i
>> whos Name Size Bytes Class Attributes
A 3x3 144 double complex
B 3x3 152 double sparse, complex
채택된 답변
추가 답변 (1개)
James Tursa
2018년 8월 14일
편집: James Tursa
2018년 8월 14일
The minimum data storage requirement formula for a double m x n sparse matrix with nnz non-zero elements, including the index data, is as follows:
bytes = max(nnz,1) * (i + (8 or 16)) + (n+1)*i
Which breaks down as follows:
nnz * i = Storing the row index of the non-zero elements (i = 4 or 8 integer size)
nnz * (8 or 16) = Storing the non-zero double element values themselves (real or complex)
(n+1) * i = Storing the cumulative number of non-zero elements thru column (i = 4 or 8)
So, for a 3x3 complex matrix with 5 non-zeros on a 64-bit system the actual minimum data storage memory will be
5 * (8 + 16) + 4 * 8 = 152
And the full matrix is of course simply
3 * 3 * 16 = 144
You can't change that ... it is simply the way MATLAB will store these variables. And this is just the data storage. The variable header information will take another 120 bytes approx as well.
All that being said, why in the world are you considering sparse matrices for this? The sample matrix you provide is neither big enough nor sparse enough for you to be considering sparse methods. Is your actual problem bigger and more sparse? Or is there another reason you are looking into using sparse?
Side note: For logical sparse matrices simply replace the (8 or 16) in the above formula with 1.
댓글 수: 3
James Tursa
2018년 8월 14일
편집: James Tursa
2018년 8월 14일
Do the math. The sparse data storage requirements for your matrix are
0.30 *(4096 * 256) * (8 + 16) + 257 * 8 = 7551803 bytes
The full data requirements are
4096 * 256 * 16 = 16777216 bytes
You would be saving about 9225413 bytes per variable. If that is worth it to you then do it. You may incur a performance penalty in your downstream code as a result depending on what you are doing with this matrix.
Steven Lord
2018년 8월 14일
Note that if you build your matrix as a full matrix then call sparse to convert it into a sparse matrix, you'll need to have enough memory to store both the full AND the sparse matrices, at least until the sparse call is finished.
If the elements in your 4096-by-256 matrix appear in some sort of pattern, consider creating vectors of row indices, column indices, and data values and calling sparse with those three vectors and the desired size of your sparse matrix as inputs. The size inputs will ensure that even if your row/column index vectors have no elements in the last row/column the matrix will be the expected size.
카테고리
도움말 센터 및 File Exchange에서 Sparse Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!