我写了这段代码来找到动态时间扭曲的最佳扭曲路径,它返回一个结构。
static inline double min (double x, double y,double z )
{
if(x < y && x < z)
return x;
else if(y < x && y < z)
return y;
else if(z < x && z < y)
return z;
}
struct backtracking_result
{
vector<int> i;
vector<int> j;
};
backtracking_result backtracking(vector<vector<double> > mGamma)
{
backtracking_result br;
cout<<"test"<<mGamma.size()<<endl;
int i=mGamma.size();
cout<<"test"<<endl;
int j=mGamma.at(0).size();
cout<<"test"<<endl;
cout<<i<<j<<endl;
do
{
cout<<"test"<<endl;
if(i == 1)
j=j-1;
else if(j == 1)
i=i-1;
else
{
if(mGamma[i-1][j] == min(mGamma[i-1][j],mGamma[i][j-1],mGamma[i-1][j-1]))
i=i-1;
else if(mGamma[i][j-1] == min(mGamma[i-1][j],mGamma[i][j-1],mGamma[i-1][j-1]))
j=j-1;
else
{
i=i-1;
j=j-1;
}
}
br.i.push_back(i);
br.j.push_back(j);
}while((i > 1)&&(j > 1));
return br;
}
当我调用这个函数时,从 gdb 调试器中得到这个错误
Program received signal SIGSEGV, Segmentation fault.
0x000000000041eafb in VectorDTW::backtracking(std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >) ()
错误在哪里?
堆栈跟踪 :
#0 0x000000000041eb17 in VectorDTW::backtracking(std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >) ()
#1 0x0000000000420195 in VectorDTW::fastdynamic(std::vector<DollarRecognizer::Point2D, std::allocator<DollarRecognizer::Point2D> >&, std::vector<DollarRecognizer::Point2D, std::allocator<DollarRecognizer::Point2D> >&) ()
#2 0x000000000041d18b in main ()
答案帮助我找出了问题,我在从矩阵计算 i 和 j 后添加了这两行,
i=i-1;
j=j-1;
现在程序不会尝试访问最后一个元素。谢谢 。但我仍然无法理解拒绝投票的原因。