【问题标题】:Newbie problems with eigen本征的新手问题
【发布时间】:2012-03-05 01:42:22
【问题描述】:

我有几个关于 eigen 的新手问题。

下面是一个小函数来说明它们。

  1. 我有一个向量,其大小将从一次迭代增长到下一次迭代,从 h=1 增长到 h=h_m,其中 h_m

    //we always have that h<h_m<n//
    //declare the matrix to its final size: //a.3
    MatrixXf xSub(h_m,p); //a.1 VectorXi Indexn1(n); //a.2 //declare the vector to its final size: //a.3 VectorXi RIndex(h_m); //a.4

    int h=10,j=0;

    while(h<h_m){ //b.0 Indexn1.setLinSpaced(n,0,n-1); //b.1 random_shuffle(Indexn1.data(),Indexn1.data()+n); //b.2 RIndex.resize(h); RIndex = Indexn1.segment(0,h); //b.3 .... .... j++; //b.4 h=ceil(j/(2.0*J)*(n-p-1)+p+1); //b.5 xSub.resize(h,NoChange); xS_mean = xSub.colwise().mean() //b.6 }

  2. b_4 行在运行时导致此错误(经过多次迭代 b.0->b.4 以上):

    eigen/Eigen/src/Core/Block.h:278: Eigen::Block::Block(XprType&, Eigen::Block::Index) [with XprType = Eigen::Matrix, int BlockRows = 1, int BlockCols = -0x00000000000000001, bool InnerPanel = false, Eigen::Block::Index = long int]: 断言`(i>=0) && (((BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) && i 中止

您能否帮助理解它的含义,以便我解决问题?

编辑:在 cbamber85 的评论之后,确实出现了错误发生在上面一行。

//index of the h smallest elements of the vector dP:
    RIndex.resize(h);
        RIndex = SSubInd(dP,h);
//constructs the corresponding sub-matrix of elements of x with index given above:
//this is the offending line.
    xSub.resize(h,NoChange);
        xSub = RSubMatrix(x,RIndex);     

查看 RSubMatrix:

MatrixXf RSubMatrix(MatrixXf& x, VectorXi& Index){
    MatrixXf xSub(Index.size(),x.cols());  
    for(int i=0;i<Index.size();i++){
        xSub.row(i)=x.row(Index(i));
    }
    return xSub;
}

:(

【问题讨论】:

  • 对不起。我忘记了它需要注册。
  • 行 b.4 不会导致该错误,因为它是一个 int 增量并且错误是 Eigen 抱怨您试图访问超出矩阵边界。
  • 谢谢 cbamber85:我会打印出 .rows() 和 h 看看有什么问题。我理解这并不意味着 b.4 导致了错误,但是当我将其注释掉时它可以工作。将尽快提供更多详细信息。

标签: c++ eigen


【解决方案1】:

错误消息表示 Eigen::Block(表示子矩阵)是用超出范围的索引构造的。块在许多地方构造,包括成员函数.row()。假设错误源于表达式xSub.row(i) = x.row(Index(i)),则意味着i 大于或等于xSubx 中的行数。

【讨论】:

  • 我认为这很到位。一个相关的问题:所以我认为矩阵 xsub 在每次迭代中都会增长。这意味着我必须在每次迭代中使用 resize() 函数。这是好习惯吗?如果没有,是否有替代方案?
猜你喜欢
  • 2011-11-11
  • 1970-01-01
  • 1970-01-01
  • 2023-01-04
  • 2012-07-16
  • 2010-12-08
  • 2011-08-13
  • 2010-12-20
  • 2010-11-05
相关资源
最近更新 更多