来自 std::vector的 arma::rowvec ;
我有一个 std::vector,我想将其转换为 arma::rowvec
我已经完成:
vector<int> x = foo();
rowvec a;
vector<int>::const_iterator iter2;
int j = 0;
for(iter2 = x.begin(); iter2 != x.end(); ++iter2) {
a(j++,0) = *iter2;
}
a.print("a");
但我得到:
error: Mat::operator(): out of bounds
terminate called after throwing an instance of 'std::logic_error'
what():
If 而不是 a(j++,0) = *iter2;
我使用<代码>a << *iter2; 在最终的 rowvec 中,我只得到最后一个元素。
I have an std::vector and I want to convert it into a arma::rowvec
I've done:
vector<int> x = foo();
rowvec a;
vector<int>::const_iterator iter2;
int j = 0;
for(iter2 = x.begin(); iter2 != x.end(); ++iter2) {
a(j++,0) = *iter2;
}
a.print("a");
but I get:
error: Mat::operator(): out of bounds
terminate called after throwing an instance of 'std::logic_error'
what():
If instead of a(j++,0) = *iter2;
I use a << *iter2;
in final rowvec, I get only the last element.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最新版本的犰狳能够直接从 std::vector 的实例构造矩阵/向量对象。
例如:
Recent versions of Armadillo are able to construct matrix/vector objects directly from instances of std::vector.
For example:
您忘记设置行向量的大小。
更正确的代码是:
还可以通过 conv_to 函数。因此,您可以这样做,而不是执行手动循环:
请注意,rowvec 是Row 的同义词。请参阅 Row 类的文档。因此,在两个代码示例中都会发生 int 到 double 的转换。如果您不希望这样,您可能希望改用irowvec。
You forgot to set the size of the row vector.
A more correct code would be:
It's also possible to convert a std::vector to an Armadillo matrix or vector via the conv_to function. So instead of doing a manual loop, you can do this:
Note that rowvec is a synonym for Row<double>. See the documentation for the Row class. As such, in both code examples there is also an int to double conversion occurring. If you don't want that, you may wish to use irowvec instead.
使用带有 aux_mem 指针的构造函数?
Using the constructor that takes an aux_mem pointer?
尝试类似的方法
根据 Armadillo Documentation rowvec 的构造函数将采用 arma::rowvec,一个 arma: :mat,一个字符串(读取 const char*)或一个初始化列表(如果您使用的是 C++11)。
Try something like
As per the Armadillo Documentation rowvec's constructor will take a arma::rowvec, a arma::mat, a string (read const char*) or an initialiser list if you are using C++11.