trying to minimize the memory of a matrix with sparse

2 vues (au cours des 30 derniers jours)
Kobi
Kobi le 13 Fév 2014
Modifié(e) : Matt J le 13 Fév 2014
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

Réponses (2)

Iain
Iain le 13 Fév 2014
Modifié(e) : Iain le 13 Fév 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.

Matt J
Matt J le 13 Fév 2014
Modifié(e) : Matt J le 13 Fév 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 commentaire
Matt J
Matt J le 13 Fév 2014
Modifié(e) : Matt J le 13 Fév 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Sparse Matrices dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by