trying to minimize the memory of a matrix with sparse
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
the meaning of the sparse function is to reduce memory usage by ignoring the zero elements inside a matrix, for some reason when i use this function the memory increase what am i doing wrong?
>> whos('hologram_r1_r2')
Name Size Bytes Class Attributes
hologram_r1_r2 376x376 2262016 double complex
>> hologram_r1_r2(abs(hologram_r1_r2)<min(min(abs(hologram_r1_r2))))=0;
>> whos('hologram_r1_r2')
Name Size Bytes Class Attributes
hologram_r1_r2 376x376 2262016 double complex
>> hologram_r1_r2=sparse(hologram_r1_r2);
>> whos('hologram_r1_r2')
Name Size Bytes Class Attributes
hologram_r1_r2 376x376 3396040 double sparse, complex
0 Kommentare
Antworten (2)
Iain
am 13 Feb. 2014
Bearbeitet: Iain
am 13 Feb. 2014
Sparse only saves memory if your array is dominated by 0. That is exactly zero, and not small numbers.
The memory used by sparse is: 4/8/16 bytes for the value of each nonzero element, plus some overhead to store the location of each nonzero element, so a full "sparse" array can take many times the memory of a genuine double array.
0 Kommentare
Matt J
am 13 Feb. 2014
Bearbeitet: Matt J
am 13 Feb. 2014
If the number of zero elements isn't at least 2/3 of the matrix (or 1/2 for a complex matrix), you won't benefit in terms of memory. Each non-zero element consumes 3 times as much memory as an ordinary matrix due to the need to store (i,j) coordinate data as well.
1 Kommentar
Matt J
am 13 Feb. 2014
Bearbeitet: Matt J
am 13 Feb. 2014
Wide sparse matrices also tend to consume more than tall ones. Here's my favorite example of an all-zero sparse matrix, which nevertheless, consumes 80 MB of memory
>> At=sparse(1,1e7); >> At=sparse(1,1e7); whos At
Name Size Bytes Class Attributes
At 1x10000000 80000024 double sparse
Siehe auch
Kategorien
Mehr zu Sparse Matrices finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!