trying to minimize the memory of a matrix with sparse

2 ビュー (過去 30 日間)
Kobi
Kobi 2014 年 2 月 13 日
編集済み: Matt J 2014 年 2 月 13 日
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

回答 (2 件)

Iain
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.

Matt J
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
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by