如何在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);
如何取回地点?例如,我希望能够做到这样的事情:
umat nonzero_index = X.locations()
有什么想法吗?
答案 0 :(得分: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;
}