在犰狳c ++中返回稀疏矩阵的位置和值
Returning locations and values of a sparse matrix in armadillo c++
如何在 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()
有什么想法吗?
关联的稀疏矩阵迭代器具有 .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;
}
如何在 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()
有什么想法吗?
关联的稀疏矩阵迭代器具有 .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;
}