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

3 views (last 30 days)
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 ...

Accepted Answer

Bruno Luong
Bruno Luong on 2 Aug 2020
Edited: Bruno Luong on 2 Aug 2020
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.

More Answers (0)

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!