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.4int 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?
EDIT: following cbamber85's remark it indeed appears that the error is happening one line above.
//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;
}
:(