Sparse Matrix Builder

バージョン 1.0.0 (4.87 KB) 作成者: Carlos Souto
Build large sparse matrices faster in Matlab!
ダウンロード: 4
更新 2023/2/17

Sparse-Matrix-Builder

Build large sparse matrices faster in Matlab!

Usage:

% reset workspace
clear, clc, close all

% example sparse matrix:
% | 1.0 0.0 0.0 0.0 2.0 |
% | 0.0 3.0 0.0 4.0 0.0 |
% | 0.0 0.0 0.0 0.0 0.0 |
% | 9.0 2.0 0.0 0.0 2.0 |

% approach 1 (slower)
% add element by element
smb = SparseMatrixBuilder(4, 5);
smb.add_val(1, 1, 1.0);
smb.add_val(1, 5, 2.0);
smb.add_val(2, 2, 3.0);
smb.add_val(2, 4, 4.0);
smb.add_val(4, 1, 4.0); % notice that
smb.add_val(4, 1, 5.0); % repeated entries are added
smb.add_val(4, 2, 2.0);
smb.add_val(4, 5, 2.0);
smb.squeeze(); % release unused storage
sparse_matrix = smb.to_matlab()

% approach 2 (faster)
% add multiple elements
smb = SparseMatrixBuilder(4, 5);
smb.add_vals([1, 1, 2], [1, 5, 2], [1.0, 2.0, 3.0]);
smb.add_vals([2, 4, 4, 4], [4, 1, 1, 2], [4.0, 4.0, 5.0, 2.0]);
smb.add_val(4, 5, 2.0); % or smb.add_vals([4], [5], [2.0]);
smb.squeeze(); % release unused storage
sparse_matrix = smb.to_matlab()

% adding multiple values is faster than adding each value individually

Notice that repeated entries are added together.

It is faster to set multiple values at once using add_vals.

Nevertheless, this implementation should perform better than Matlab's sparse during a loop building process.

Test results

a b

引用

Carlos Souto (2024). Sparse Matrix Builder (https://github.com/carlos-souto/Sparse-Matrix-Builder), GitHub. 取得済み .

MATLAB リリースの互換性
作成: R2022b
すべてのリリースと互換性あり
プラットフォームの互換性
Windows macOS Linux
タグ タグを追加

Community Treasure Hunt

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

Start Hunting!

GitHub の既定のブランチを使用するバージョンはダウンロードできません

バージョン 公開済み リリース ノート
1.0.0

この GitHub アドオンでの問題を表示または報告するには、GitHub リポジトリにアクセスしてください。
この GitHub アドオンでの問題を表示または報告するには、GitHub リポジトリにアクセスしてください。