来自 std::vector的 arma::rowvec ;

发布于 2025-01-05 00:22:16 字数 533 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深海里的那抹蓝 2025-01-12 00:22:16

最新版本的犰狳能够直接从 std::vector 的实例构造矩阵/向量对象。

例如:

std::vector<double> X(5);

// ... process X ...

arma::vec Y(X);
arma::mat M(X);

Recent versions of Armadillo are able to construct matrix/vector objects directly from instances of std::vector.

For example:

std::vector<double> X(5);

// ... process X ...

arma::vec Y(X);
arma::mat M(X);
℡Ms空城旧梦 2025-01-12 00:22:16

您忘记设置行向量的大小。

更正确的代码是:

vector<int> x = foo();
rowvec a(x.size());
... rest of your code ...

还可以通过 conv_to 函数。因此,您可以这样做,而不是执行手动循环:

vector<int> x = foo();
rowvec a = conv_to<rowvec>::from(x);

请注意,rowvecRow的同义词。请参阅 Row 类的文档。因此,在两个代码示例中都会发生 intdouble 的转换。如果您不希望这样,您可能希望改用irowvec

You forgot to set the size of the row vector.

A more correct code would be:

vector<int> x = foo();
rowvec a(x.size());
... rest of your code ...

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:

vector<int> x = foo();
rowvec a = conv_to<rowvec>::from(x);

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.

∞琼窗梦回ˉ 2025-01-12 00:22:16

使用带有 aux_mem 指针的构造函数?

 rowvec a(x.pointer, x.size()); 

Using the constructor that takes an aux_mem pointer?

 rowvec a(x.pointer, x.size()); 
可爱暴击 2025-01-12 00:22:16

尝试类似的方法

vector<int> x = foo();
vector<int>::const_iterator iter2;
stringstream buffer;
for(iter2 = x.begin(); iter2 != x.end(); ++iter2) 
{
   buffer  << *iter << " ";    
}
// To remove the extra trailing space, not sure if it is needed or not
string elements = buffer.str().substr(0,buffer.str().length()-1);

rowvec a = rowvec(elements.c_str());

根据 Armadillo Documentation rowvec 的构造函数将采用 arma::rowvec,一个 arma: :mat,一个字符串(读取 const char*)或一个初始化列表(如果您使用的是 C++11)。

Try something like

vector<int> x = foo();
vector<int>::const_iterator iter2;
stringstream buffer;
for(iter2 = x.begin(); iter2 != x.end(); ++iter2) 
{
   buffer  << *iter << " ";    
}
// To remove the extra trailing space, not sure if it is needed or not
string elements = buffer.str().substr(0,buffer.str().length()-1);

rowvec a = rowvec(elements.c_str());

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文