矩阵计算

发布于 2024-11-24 05:25:36 字数 1564 浏览 2 评论 0原文

我有一个大的二维矩阵 A2D,其中列秩和行秩始终相等,并除以 4。我想将列秩和行秩减少到各自的四分之一(1/4)以形成另一个矩阵 B2D。每个B2D元素是A2D的4x4子矩阵的平均值。为了清楚地说明我想要做什么,我以一个简单的 8x8 矩阵为例,并提供以下代码片段供您参考。我的解决方案非常笨拙。您能告诉我另一个性能更好的解决方案吗?先感谢您。

int arr[8][8] =     
{   
   {11, 12, 13, 14, 15, 16, 17, 18}, 
   {21, 22, 23, 24, 25, 26, 27, 28},
   {31, 32, 33, 34, 35, 36, 37, 38},
   {41, 42, 43, 44, 45, 46, 47, 48},
   {51, 52, 53, 54, 55, 56, 57, 58},
   {61, 62, 63, 64, 65, 66, 67, 68},
   {71, 72, 73, 74, 75, 76, 77, 78},
   {81, 82, 83, 84, 85, 86, 87, 88}
};

int** pColAvg = new int* [8];

for (int i = 0; i < 8; i++)
    pColAvg[i] = new int[2];

for (int nRow = 0; nRow < 8; nRow + 4)
{   
    for (int nCol = 0; nCol < 8; nCol + 4)
    {  
        int Avg = 0;
        Avg += arr[nRow][nCol];
        Avg += arr[nRow][nCol + 1];
        Avg += arr[nRow][nCol + 2];
        Avg += arr[nRow][nCol + 3];
        Avg /= 4;

        pColAvg[nRow][nCol/4] = Avg;
    }
}

int** pAvgArray = new int* [2];

for (int i = 0; i < 2; i++)
    pAvgArray[i] = new int[2];

for (int nRow = 0; nRow < 8; nRow + 4)
{    
    for (int nCol = 0; nCol < 2; nCol++)
    {   
        int Avg = 0;
        Avg += pColAvg[nRow][nCol];
        Avg += pColAvg[nRow + 1][nCol];
        Avg += pColAvg[nRow + 2][nCol];
        Avg += pColAvg[nRow + 3][nCol];
        Avg /= 4;

        pAvgArray[nRow/4][nCol] = Avg;
    }
}

for (int i = 0; i < 8; i++)     
    delete [] pColAvg[i]; 

delete [] pColAvg; 

for (int i = 0; i < 2; i++)     
    delete [] pAvgArray[i]; 

delete [] pAvgArray; 

I have a large 2D matrix A2D, which the column rank and the row rank are always equal, and evenly divided by 4. I want to reduce both the column rank and the row rank to their respective quater (1/4) to form another matrix B2D. Each B2D element is an average of 4x4 sub-matrix of A2D. To account for clearly what I want to do, I take a simple 8x8 matrix for example, and provide the following code snippet for your reference. My solution is very clumsy. Would you please show me aother solution with better performance. Thank you in advance.

int arr[8][8] =     
{   
   {11, 12, 13, 14, 15, 16, 17, 18}, 
   {21, 22, 23, 24, 25, 26, 27, 28},
   {31, 32, 33, 34, 35, 36, 37, 38},
   {41, 42, 43, 44, 45, 46, 47, 48},
   {51, 52, 53, 54, 55, 56, 57, 58},
   {61, 62, 63, 64, 65, 66, 67, 68},
   {71, 72, 73, 74, 75, 76, 77, 78},
   {81, 82, 83, 84, 85, 86, 87, 88}
};

int** pColAvg = new int* [8];

for (int i = 0; i < 8; i++)
    pColAvg[i] = new int[2];

for (int nRow = 0; nRow < 8; nRow + 4)
{   
    for (int nCol = 0; nCol < 8; nCol + 4)
    {  
        int Avg = 0;
        Avg += arr[nRow][nCol];
        Avg += arr[nRow][nCol + 1];
        Avg += arr[nRow][nCol + 2];
        Avg += arr[nRow][nCol + 3];
        Avg /= 4;

        pColAvg[nRow][nCol/4] = Avg;
    }
}

int** pAvgArray = new int* [2];

for (int i = 0; i < 2; i++)
    pAvgArray[i] = new int[2];

for (int nRow = 0; nRow < 8; nRow + 4)
{    
    for (int nCol = 0; nCol < 2; nCol++)
    {   
        int Avg = 0;
        Avg += pColAvg[nRow][nCol];
        Avg += pColAvg[nRow + 1][nCol];
        Avg += pColAvg[nRow + 2][nCol];
        Avg += pColAvg[nRow + 3][nCol];
        Avg /= 4;

        pAvgArray[nRow/4][nCol] = Avg;
    }
}

for (int i = 0; i < 8; i++)     
    delete [] pColAvg[i]; 

delete [] pColAvg; 

for (int i = 0; i < 2; i++)     
    delete [] pAvgArray[i]; 

delete [] pAvgArray; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薔薇婲 2024-12-01 05:25:36

我认为您的解决方案可能是不正确的(即使平均而言您的意思是精确平均值的下限)。这是我的解决方案:

int** solveIt(int **arr, int n){
    int **result = new int*[n/4];
    for(int i=0; i<n; i+=4){
        result[i] = new int[n/4];
        for(int j=0; j<n; j+=4){
            int sum = 0;
            for(int k=0; k<4; k++)
                for(int q=0; q<4; q++)
                    sum += arr[i+k][j+q];
            result[i/4][j/4] = sum/16;
        }
    }
    return result;
}

这是一个函数,它接受数组和该数组的大小,并返回结果数组。

编辑:
以及您的解决方案不起作用的示例:

1 1 1 1
1 1 1 2
1 1 1 1
1 1 0 1

答案是:

1

但您的解决方案将给出:

0

I think that your solution can be incorrect (even if by average you mean floor of exact average). Here is my solution:

int** solveIt(int **arr, int n){
    int **result = new int*[n/4];
    for(int i=0; i<n; i+=4){
        result[i] = new int[n/4];
        for(int j=0; j<n; j+=4){
            int sum = 0;
            for(int k=0; k<4; k++)
                for(int q=0; q<4; q++)
                    sum += arr[i+k][j+q];
            result[i/4][j/4] = sum/16;
        }
    }
    return result;
}

this is function that takes array and size of that array, and returns result array.

Edit:
and the example where your solution doesn't work:

1 1 1 1
1 1 1 2
1 1 1 1
1 1 0 1

The answer is:

1

but your solution will give:

0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文