在犰狳C中返回稀疏矩阵的位置和值 [英] Returning locations and values of a sparse matrix in armadillo c++

查看:85
本文介绍了在犰狳C中返回稀疏矩阵的位置和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I get an array of nonzero locations (indices) and values of a sparse matrix in Armadillo C++?

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

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()

有什么想法吗?

推荐答案

关联的稀疏矩阵迭代器具有 .row() .col()函数:

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;
  }

这篇关于在犰狳C中返回稀疏矩阵的位置和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆