Which is more efficient when applied to a sparse matrix, find or logical indexing ?
3 views (last 30 days)
Show older comments
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 Comment
Accepted Answer
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.
0 Comments
More Answers (0)
See Also
Categories
Find more on Sparse Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!