Returned locations and sparse matrix values ​​in armadillo C ++

How to get an array of non-zero locations (indices) and sparse matrix values ​​in Armadillo C ++?

So far, I can easily build a sparse matrix with a set of locations (as an umat object) and values ​​(as a vec object):

// batch insertion of two values at (5, 6) and (9, 9) umat locations; locations << 5 << 9 << endr << 6 << 9 << endr; vec values; values << 1.5 << 3.2 << endr; sp_mat X(locations, values, 9, 9); 

How do I get places back? For example, I want to be able to do something like this:

 umat nonzero_index = X.locations() 

Any ideas?

+5
source share
1 answer

The related sparse matrix matrix iterator has the functions .row() and .col() :

 sp_mat::const_iterator start = X.begin(); sp_mat::const_iterator end = X.end(); for(sp_mat::const_iterator it = start; it != end; ++it) { cout << "location: " << it.row() << "," << it.col() << " "; cout << "value: " << (*it) << endl; } 
+14
source

Source: https://habr.com/ru/post/1215114/


All Articles