Which is more efficient when applied to a sparse matrix, find or logical indexing ?

4 次查看(过去 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 ...

采纳的回答

Bruno Luong
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 个)

类别

Help CenterFile Exchange 中查找有关 Sparse Matrices 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by