我有几个关于本征的新手问题。
下面是一个小函数来说明它们。
我有一个向量,其大小将从一次迭代增长到下一次迭代,从 h=1 到 h=h_m 且 h_m<n (n 给定)。我找不到和等效于 .reserve() 特征对象的函数,这意味着我必须在每次迭代时调整这些对象的大小。你能建议一种避免这种情况的方法吗?
//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 }
b_4 行在运行时导致此错误(在上面的 b.0->b.4 多次迭代之后):
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, , true>::Index = long int]: 断言 `(i>=0) && ( ((BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) && i<xpr.rows()) ||( (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) && i<xpr.cols()))' 失败。
中止
你能帮助理解它的含义,以便我解决问题吗?
编辑:在 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;
}
:(