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

问题描述 投票:1回答:2

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

在每次循环迭代中,我需要将循环+ 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 --------------------------------

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

c++ c signal-processing
2个回答
1
投票

正如Vimal的回答中所提到的,你想要总结2D矩阵的对角线。如果您更改当前代码以基于2D数组xCorr而不是1D数组twoDtemp设置temp,则更加明显:

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开始(即数组维度的限制),并且一个下降,一个上升,直到另一侧达到限制。

因此,您需要一个外部循环来遍历每个对角线,以及一个向上/向下每个索引的内部循环。

你可以这样做:

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。


2
投票

我会暗示一下 -

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的所有指数。

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

© www.soinside.com 2019 - 2024. All rights reserved.