我有一个函数,其中两个向量相互比较,程序从向量中找到平方差的总和。
double Search::NNS(vector<vector<double>> bb, vector<vector<double>> aa)
{
int M = 768; int N = 1024;
int R = 49; int C = 36;
//double SSD[] = MainVectorBlock[] - WallyVector[];
//double SSD[] = SSD[] * SSD[];
//sum = sum + SSD[];
vector<vector<double>> &MainIMG = bb;
vector<vector<double>> &WallyIMG = aa;
double *SSD = new double[R*C];
double sum = 0;
for (int bx = 0; bx < M; bx += R)
for (int by = 0; by < N; by += C)
{
Compare = new double*[R];
for (int x = 0; ((x < R) && ((bx + x) < M)); ++x)
{
Compare[x] = new double[R];
for (int y = 0; ((y < C) && ((by + y) < N)); ++y)
{
if ((bx + x) >= M)
{
cout << Compare[bx + x] << Compare[by + y] << " ";
}
//cout << MainIMG[bx + x][by + y] << " ";
Compare[x][y] = MainIMG[bx + x][by + y] - WallyIMG[x][y];
Compare[x][y] = Compare[x][y] * Compare[x][y];
//sum += Compare[x][y];
SSD[R*C] += Compare[x][y];
//SSD[R*C] = sum;
//cout << Compare[x][y] << " ";
}
}
//cout << "\n\n\n" << endl;
//cout << sum << endl;
//cout << SSD[R*C] << "\t" << sum << endl;
for (int i = 0; i < R*C; i++)
{
for (int j = 0; j < R*C; j++)
{
if (SSD[i] > SSD[j])
{
int temp = SSD[i];
SSD[i] = SSD[j];
SSD[j] = temp;
}
}
}
}
for (int a = 0; a < R*C; a++)
{
cout << SSD[a] << endl;
}
return 0;
}
我可以显示所有差值平方的总和,但是当我尝试按升序对值进行排序时,我会继续获得该值-6.27744e + 66。
我试图更改循环并将其放置在整个主要for循环中,但我仍然继续获得该值。
最佳答案
double *SSD = new double[R*C];
您已经分配了内存,但从未将其初始化为某个值。然后,您直接使用了它:
SSD[R*C] += Compare[x][y];
在开始向其添加值之前,将
SSD
的所有项目初始化为0
。关于c++ - C++按升序对指针数组进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41280606/