trying to minimize the memory of a matrix with sparse
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (2개)
Iain
2014년 2월 13일
편집: Iain
2014년 2월 13일
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
Matt J
2014년 2월 13일
편집: Matt J
2014년 2월 13일
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
Matt J
2014년 2월 13일
편집: Matt J
2014년 2월 13일
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
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!