gpt4 book ai didi

armadillo - 在 Armadillo C++ 中返回稀疏矩阵的位置和值

转载 作者:行者123 更新时间:2023-12-04 10:03:48 28 4
gpt4 key购买 nike

如何在 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 - 在 Armadillo C++ 中返回稀疏矩阵的位置和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28980228/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com