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

3 Ansichten (letzte 30 Tage)
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 ...

Akzeptierte Antwort

Bruno Luong
Bruno Luong am 2 Aug. 2020
Bearbeitet: Bruno Luong am 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.

Weitere Antworten (0)

Kategorien

Mehr zu Sparse Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by