在armadillo c ++中返回稀疏矩阵的位置和值

kch*_*462 7 armadillo

如何在Armadillo C++中获得稀疏矩阵的非零位置(索引)和值的数组?

到目前为止,我可以轻松地构造一个稀疏矩阵,其中包含一组位置(作为umat对象)和值(作为vec对象):

// 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);
Run Code Online (Sandbox Code Playgroud)

我该如何取回地点?例如,我希望能够做到这样的事情:

umat nonzero_index = X.locations()
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

mta*_*all 16

相关联的稀疏矩阵迭代器有.row().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;
  }
Run Code Online (Sandbox Code Playgroud)