我必须执行2d矩阵卷积,但是我使用1d数组存储矩阵。这些1d数组的大小为NumRows * NumCols
,并保存矩阵的值。
我已经编写了以下代码来进行2D卷积,但结果却是错误的。有人可以帮我解决问题吗?
mat_A is of size NumRows_first * NumCols_first.
mat_B is of size NumRows_second * NumCols_second.
mat_C is the output array of size (NumRows_first + NumRows_second - 1) * (NumCols_first + NumCols_second - 1)
我写的代码是:
int c_x = NumRows_second / 2;
int c_y = NumCols_second / 2;
int ii = 0;
int jj = 0;
for(i=0; i<(NumRows_first + NumRows_second - 1) * (NumCols_second + NumCols_first - 1); i++)
{
for(j=0; j<(NumRows_first + NumRows_second - 1) * (NumCols_second + NumCols_first - 1); j++)
{
for(int m=0; m<NumRows_second - 1; m++)
{
for(int k=0; k<NumCols_second - 1; k++)
{
ii = i + (m - c_x);
jj = j + (k - c_y);
if(ii >= 0 && ii < NumRows_first && jj>=0 && jj<NumCols_first)
mat_C[i] += mat_A[ii*jj] * mat_B[m*k];
}
}
}
}
数组均为double