How do I get an array of nonzero locations (indices) and values of a sparse matrix in Armadillo C++?
So far, I can easily construct a sparse matrix with a set of locations (as a 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 back the locations? For example, I want to be able to do something like this:
umat nonzero_index = X.locations()
Any ideas?
The associated sparse matrix iterator has .row()
and .col()
functions:
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With