Query regarding a sparse complex matrix
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
D_coder
el 14 de Ag. de 2018
Comentada: Steven Lord
el 14 de Ag. de 2018
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
0 comentarios
Respuesta aceptada
Stephan
el 14 de Ag. de 2018
Editada: Stephan
el 14 de Ag. de 2018
Hi,
the resulting size of the sparse matrix depends from the number of zero entries. Consider this:
A = randi(10,100);
A(A<=7) = 0;
B = sparse(A);
whos A B
Name Size Bytes Class Attributes
A 100x100 80000 double
B 100x100 49096 double sparse
The code above produced a sparse matrix which has about 60% size then the normal one. There should be about 70% zeros in it.
If we only have about 30% zeros the situation changes:
A = randi(10,100);
A(A<=3) = 0;
B = sparse(A);
whos A B
Name Size Bytes Class Attributes
A 100x100 80000 double
B 100x100 113240 double sparse
Now the size of B is about 40% bigger than A.
EDIT - Now the same example using complex numbers:
about 70% zeros:
R = randi(10,100);
I = ones(100);
C = complex(R,I);
C(real(C)<=7) = 0;
D = sparse(C);
whos C D
gives:
Name Size Bytes Class Attributes
C 100x100 160000 double complex
D 100x100 71944 double sparse, complex
and the same with about 30% zeros:
R = randi(10,100);
I = ones(100);
C = complex(R,I);
C(real(C)<=3) = 0;
D = sparse(C);
whos C D
gives:
Name Size Bytes Class Attributes
C 100x100 160000 double complex
D 100x100 168928 double sparse, complex
The values differ, but the fundamental effect is the same.
Best regards
Stephan
2 comentarios
Más respuestas (1)
James Tursa
el 14 de Ag. de 2018
Editada: James Tursa
el 14 de Ag. de 2018
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 comentarios
James Tursa
el 14 de Ag. de 2018
Editada: James Tursa
el 14 de Ag. de 2018
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
el 14 de Ag. de 2018
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.
Ver también
Categorías
Más información sobre Sparse Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!