Modifying a large sparse matrix efficiently
古いコメントを表示
I have a lot of zero elements in the matrix but I also I have a lot of elements that are small enough that I want to zero them out. What is the best way to do this? Keep in mind that it's a sparse matrix so if I index into this, wouldn't it take too much time? If I convert it back to a full matrix, that may not be memory efficient too.
採用された回答
その他の回答 (3 件)
Walter Roberson
2013 年 3 月 12 日
tol = 0.001;
[i, j, s] = find(YourSparseMatrix);
idx = abs(s) < tol;
i(idx) = [];
j(idx) = [];
s(idx) = [];
YourSparseMatrix = sparse(i, j, s); %write over it with new sparse matrix
James Tursa
2013 年 3 月 14 日
1 投票
I couldn't resist making a small mex function to do this. In addition to zeroing out small values, it can zero out values within a user-defined range (not necessarily small) and zero out or replace nan values. Works with real or complex matrices, and can generate a new matrix result or operate in-place. For in-place operations, it will throw an error if matrix is shared to avoid unintended side effects (but this behavior can be over-ridden). You can find it here:
The advantage of this mex implementation is that it doesn't generate potentially large intermediate arrays to get the job done.
カテゴリ
ヘルプ センター および File Exchange で Sparse Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!