Reputation: 5789
I have several newbie questions concerning eigen.
Below is a small function to illustrate them.
I have a vector whose size will growth from one iteration to the next, from h=1 to h=h_m with h_m<n (n given). I couldn't find and equivalent to the .reserve() function for eigen objects, meaning i have to resize these object at each iteration. Could you advise of a way to avoid this?
//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
}
the line b_4 causes this error at run time (after several iterations of b.0->b.4 above):
eigen/Eigen/src/Core/Block.h:278: Eigen::Block<XprType, BlockRows, BlockCols, InnerPanel, true>::Block(XprType&, Eigen::Block<XprType, BlockRows, BlockCols, InnerPanel, true>::Index) [with XprType = Eigen::Matrix<float, -0x00000000000000001, -0x00000000000000001>, int BlockRows = 1, int BlockCols = -0x00000000000000001, bool InnerPanel = false, Eigen::Block<XprType, BlockRows, BlockCols, InnerPanel, true>::Index = long int]: Assertion `(i>=0) && ( ((BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) && i<xpr.rows()) ||((BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) && i<xpr.cols()))' failed.
Aborted
Can you help understanding what it means so I can solve the problem?
//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);
Looking unto 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;
}
:(
Upvotes: 0
Views: 5350
Reputation: 4542
The error message means that an Eigen::Block (which represent a submatrix) was constructed with indices that are out of bound. Blocks are constructed in many places, including the member function .row()
. Assuming that the error originates in the expression xSub.row(i) = x.row(Index(i))
, it means that i
is greater than or equal to the number of rows in either xSub
or x
.
Upvotes: 3