The memory use of a sparse matrix depends on its history

3 ビュー (過去 30 日間)
Steve Van Hooser
Steve Van Hooser 2025 年 2 月 26 日
コメント済み: Steve Van Hooser 2025 年 2 月 26 日
The amount of memory used by a sparse matrix depends on its history. In a project this week, a matrix that could have consumed a few bytes was consuming gigabytes of memory.
Demonstration:
J = sparse(zeros(2,2));
J2 = [J zeros(2,2); zeros(1,4)];
J3 = sparse(zeros(3,4)); % same data as J2
isequal(J2,J3)
%ans =
% logical
% 1
whos('J2')
% Name Size Bytes Class Attributes
% J2 3x4 88 double sparse
whos('J3')
% Name Size Bytes Class Attributes
% J3 3x4 56 double sparse
%
% 88 > 56!

採用された回答

Steve Van Hooser
Steve Van Hooser 2025 年 2 月 26 日

その他の回答 (1 件)

Steven Lord
Steven Lord 2025 年 2 月 26 日
The amount of memory required by a sparse matrix is not just a function of the number of rows and columns but also the number of non-zero elements stored and the number of non-zero locations allocated for storage. In the case of your J2 and J3, they are the same size but have different numbers of locations allocated for storage.
J = sparse(zeros(2,2));
J2 = [J zeros(2,2); zeros(1,4)];
J3 = sparse(zeros(3,4)); % same data as J2
numberOfNonzerosInJ2 = nnz(J2)
numberOfNonzerosInJ2 = 0
numberOfNonzerosAllocatedInJ2 = nzmax(J2)
numberOfNonzerosAllocatedInJ2 = 3
numberOfNonzerosInJ3 = nnz(J3)
numberOfNonzerosInJ3 = 0
numberOfNonzerosAllocatedInJ3 = nzmax(J3)
numberOfNonzerosAllocatedInJ3 = 1
If you know how many non-zero elements you're ultimately going to want your sparse matrix to contain, use the spalloc function to preallocate it.
J4 = spalloc(height(J2), width(J2), 2); % Matrix is same size as J2, but only 2 nonzero locations
whos J*
Name Size Bytes Class Attributes J 2x2 40 double sparse J2 3x4 88 double sparse J3 3x4 56 double sparse J4 3x4 72 double sparse
  2 件のコメント
James Tursa
James Tursa 2025 年 2 月 26 日
編集済み: James Tursa 2025 年 2 月 26 日
As a follow up, when encountered in operations MATLAB will shrink the resulting sparse memory allocation to the minimum size necessary. I am unaware of any published rules for what operations will trigger this, but it does seem to happen for the "usual" stuff. E.g.,
S = spalloc(1000,1000,10000);
T = S + sparse(0);
whos
Name Size Bytes Class Attributes S 1000x1000 168008 double sparse T 1000x1000 8024 double sparse
Steve Van Hooser
Steve Van Hooser 2025 年 2 月 26 日
Thanks Steven and James

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

カテゴリ

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