Which is more efficient when applied to a sparse matrix, find or logical indexing ?
6 ビュー (過去 30 日間)
古いコメントを表示
Given a sparse matrix, I would like to get its non-zero entries. I would like to know if using find function is an efficient way to do this or are there more efficient ways. Moreover, once I have a sparse matrix S in [rows, cols, vals] format with size nnz(S), how much cost is associated with executing sparse(rows, cols, vals, m, n). Thank you ...
1 件のコメント
採用された回答
Bruno Luong
2020 年 8 月 2 日
編集済み: Bruno Luong
2020 年 8 月 2 日
For sparse matrix using FIND to get non-zero elements is the best - bar none. It's design for it and the data are internally ranged such a way that MATLAB returns index/value effortlessly. Just do it directly on the matrix:
[rows, cols, vals] = find(S);
(However if you to find with some addition logical operator (unitary/binary) on the matrix, this is another matter.)
The cost of
sparse(rows, cols, vals, m, n)
is quite reasonable, and it's indeed the best way to build sparse matrix. Avoid to update elements of a prebuilt sparse matrix iteratively. The later method is quite innefficient.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!