This forum has been archived. All content is frozen. Please use KDE Discuss instead.

handy way to determine the sparsity fraction of a Matrix?

Tags: None
(comma "," separated)
bravegag
Registered Member
Posts
52
Karma
0
Hello,

I'm interested in finding out the sparsity fraction of a MatrixXd without having to write helper function loops etc i.e. count the zeros and then get:
sparsity fraction = zeroCount/M*N

Is there some provision in Eigen for this use-case somewhere in whatever form? I need it to automatically switch between dense and sparse solvers.

TIA,
Best regards,
Giovanni
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
That's easy: double(mat.nonZeros())/double(mat.size())
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
hm wait, the above works for a sparse matrix, so for a MatrixXd: double((mat.array()!=0).count()) / double(mat.size())


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]