Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning locations and values of a sparse matrix in armadillo c++

Tags:

armadillo

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?

like image 720
Bluegreen17 Avatar asked Mar 11 '15 06:03

Bluegreen17


1 Answers

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;
  }
like image 135
mtall Avatar answered Oct 21 '22 02:10

mtall