在每次循环迭代时一起添加可变数量的数组元素

Add variable number of array elements together at each loop iteration

我正在寻找一个循环结构来完成以下任务:

在每次循环迭代中,我需要添加一个循环+1 个索引,直到 maxArrSize(在本例中为 5),然后从那里添加 --maxArrSize 个索引,直到 maxArrSize==0。 这是两个数组的互相关,我已经处理了每个数组移位的加法。

这里有一个缩小的例子来帮助解释我正在寻找的东西:

// ------------------------- DON'T FOCUS ON THIS ------------------------
int8 a[5] = { 1, 5, 4, 7, 8 };
int8 b[5] = { 2, 0, 1, 3, 3 };
int maxArrSize = sizeof(a) >= sizeof(b) ? sizeof(a) : sizeof(b);
int8 twoDtemp[5][5];
int8 temp[28] = { };
int8 xCorr[5*2-1] = { };
int x = 5;

// Fill the multiplication matrix
for (int loop = 0; loop < maxArrSize; loop++)
{
    for (int nested = 0; nested < maxArrSize; nested++)
    {
        twoDtemp[loop][nested] = b[iterate] * a[nested];
    }
    iterate--;
}
// Copy into a single dimension array
memcpy(temp, twoDtemp, sizeof(a)*sizeof(a));

// ------------------------- END DON'T FOCUS ON THIS --------------------


// ---------------------------- FOCUS ON THIS ---------------------------
// Below is where I want create a loop that will do the below for me.
// So I don't have to write the entire addition table.


// Cross-Correlate
//xCorr[0] = temp[0]; // +sizeof(b)-1 right, +1 down
//xCorr[1] = temp[1]+temp[5];
//xCorr[2] = temp[2]+temp[6]+temp[10];
//xCorr[3] = temp[3]+temp[7]+temp[11]+temp[15];
//xCorr[4] = temp[4]+temp[8]+temp[12]+temp[16]+temp[20];
//xCorr[5] = temp[9]+temp[13]+temp[17]+temp[21]; // +sizeof(b)-1 right, +sizeof(b) down
//xCorr[6] = temp[14]+temp[18]+temp[22];
//xCorr[7] = temp[19]+temp[23];
//xCorr[8] = temp[24];
// ------------------------ END FOCUS ON THIS ---------------------------

// ****************
// **** UPDATE ****
// ****************
// --------------------------- SOLUTION ---------------------------------
for (int split = 0; split < 2 * x - 1; ++split)
{
    int z = (split < x) ? 0 : split - x + 1;

    for (int j = z; j <= split - z; ++j)
    {
        xCorr[split] += twoDtemp[j][split - j];
    }
}
// ------------------------ END SOLUTION --------------------------------

感谢您的帮助,我会根据需要进行更多解释。

我给个提示-

0   1  2  3  4
5   6  7  8  9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24

所以 xCorr 只是指数的对角线和。查看 xCorr[2] = temp[2] + temp[6] + temp[10] 现在看上面矩阵中的数字 2,6,10,你看到了吗? xCorr.

的所有索引都类似

所以这只是关于以这种方式找到对角线和。试着找找逻辑,这将是很好的大脑锻炼。

Vimal 的回答中提到,你想求和 2D 矩阵的对角线。如果您将当前代码更改为基于二维数组 twoDtemp 而不是一维数组 temp:

来设置 xCorr,这一点会更加明显
xCorr[0] =  twoDtemp[0][0];
xCorr[1] =  twoDtemp[0][1] + twoDtemp[1][0];
xCorr[2] =  twoDtemp[0][2] + twoDtemp[1][1] + twoDtemp[2][0];
xCorr[3] =  twoDtemp[0][3] + twoDtemp[1][2] + twoDtemp[2][1] + twoDtemp[3][0];
xCorr[4] =  twoDtemp[0][4] + twoDtemp[1][3] + twoDtemp[2][2] + twoDtemp[3][1] + twoDtemp[4][0];
xCorr[5] =  twoDtemp[1][4] + twoDtemp[2][3] + twoDtemp[3][2] + twoDtemp[4][1];
xCorr[6] =  twoDtemp[2][4] + twoDtemp[3][3] + twoDtemp[4][2];
xCorr[7] =  twoDtemp[3][4] + twoDtemp[4][3];
xCorr[8] =  twoDtemp[4][4];

请注意,两个索引之一从 0 或 4(即数组维度的限制)开始,一个下降,一个上升,直到另一侧达到限制。

所以你需要一个外循环来遍历每个对角线,还有一个内循环 up/down 每个索引。

您可以按如下方式进行:

int matrix[len][len];    // len is an int defined elsewhere

...

// populate matrix

...

int i, j
for (i=0; i < len*2 - 1; i++) {
    int min =  (i - (len-1) > 0) ? i - (len-1) : 0;
    xCorr[i] = 0;
    for (j=0; j<=i-min && j<len-min; j++) {
        xCorr[i] += matrix[j+min][i-min-j];
    }
} 

min 变量指定起始 X 索引。在第一个 len 对角线上它是 0,之后它是对角线数字减去 len.

内层循环从 min 开始 X 索引,从对角线数字减去 min 开始 Y 索引。然后 X 指数上升,Y 指数下降,直到 X 指数达到 len-1 或 Y 指数达到 0.