简体   繁体   English

二维数组平均 - 卷积 - C 程序

[英]2D Array Average - Convolution - C Program

在此处输入图像描述

I need help with my program.我的程序需要帮助。 I need it to calculate the 3x3 average and then go and and calculate the next.我需要它来计算 3x3 平均值,然后是 go,然后计算下一个。 This is what i got so far, it's only to calculate the average of all and now I'm stuck…这是我到目前为止所得到的,它只是计算所有的平均值,现在我被困住了……

#include <stdio.h>
#define ROWS 5
#define COLS 7

int main(void){
    float in_sum = 0;
    float *in_matrix[ROWS][COLS];
    float in_avg;

    float matr[ROWS][COLS]={{1.5, 5, 6, 12, 13, 7, 80},
                            {50, 6.5, 23, 77, 17, 8.5, 28},
                            {43.5, 78, 8, 9, 34.5, 10, 95},
                            {75, 44, 40, 29, 39, 5, 99.5},
                            {18, 86, 68, 92, 10.5, 11, 4}};

    printf("Matrix Input:\n");

        for(int i = 0; i < ROWS; i++){
            for (int j = 0; j < COLS; j++){
                printf("%.2f ", matr[i][j]);
                    if(j==6){
                        printf("\n");
                    }
            }
        }
        printf("\nMatrix Output: \n");
        int j = 0, nr = 3, nc = 3;
        for (int i = 0; i < nr; i++){
            for(j = 0; j < nc; j++){
                in_sum = in_sum + matr[i][j];
            }
        }
        in_avg = in_sum/(ROWS*COLS);
        for (int i=0; i< ROWS; i++){
            for (int j=0; j< COLS; j++){
            printf("%.2f", in_avg);
            }
            printf("\n");
        }
        in_matrix[ROWS][COLS] = &in_sum;
    return 0;
}

The code you shared isn't even close to being working.您共享的代码甚至无法正常工作。 Try this:尝试这个:

#include <stdio.h>

#define COLS 7
#define OVER 3
#define ROWS 5

float sum(size_t cols, float *matrix) {
    float s = 0;
    for(size_t row = 0; row < OVER; row++) {
        for(size_t col = 0; col < OVER; col++) {
            s += matrix[row * cols + col];
        }
    }
    return s / (OVER * OVER);
}

int main(void) {
    float matrix[ROWS][COLS] = {
        {1.5, 5, 6, 12, 13, 7, 80},
        {50, 6.5, 23, 77, 17, 8.5, 28},
        {43.5, 78, 8, 9, 34.5, 10, 95},
        {75, 44, 40, 29, 39, 5, 99.5},
        {18, 86, 68, 92, 10.5, 11, 4}
    };
    for(size_t row=0; row < ROWS - OVER + 1; row++) {
        for(size_t col=0; col < COLS - OVER + 1; col++) {
            printf("%.1f%c",
                sum(COLS, &matrix[row][col]),
                col + 1 < COLS - OVER + 1 ? ' ' : '\n');
        }
    }
}


and example run:和示例运行:

24.6 24.9 22.2 20.9 32.6
40.9 34.9 30.7 25.4 37.4
51.2 50.4 36.7 26.7 34.3

As the input matrix is hard-coded this works, but if you need a generic function you should check that the input is at least a OVER x OVER matrix.由于输入矩阵是硬编码的,因此可以正常工作,但如果您需要通用的 function,则应检查输入是否至少为 OVER x OVER 矩阵。

The sum() performs 8 additions for matrix[0][0] and then another 8 additions for the matrix[0][1] . sum()matrix[0][0]执行 8 次加法,然后对matrix[0][1]执行另外 8 次加法。 We could clearly do better.我们显然可以做得更好。 For example by retaining the last sum and subtract what is no longer in the window and add what now is.例如,通过保留最后的总和并减去 window 中不再存在的内容并添加现在的内容。 Caching of partial results.缓存部分结果。

Updating the Sum value when the computation range moves to right.当计算范围向右移动时更新Sum值。

#define ROWS (5)
#define COLS (7)

float SumOf3Elem( const float *p ){ return p[0]+p[1]+p[2];  }
float Add4th_Sub1st( float S, const float *p ){ return S + p[3] - p[0]; }

int main()
{
    float M[ROWS][COLS] = {
        {1.5, 5, 6, 12, 13, 7, 80},
        {50, 6.5, 23, 77, 17, 8.5, 28},
        {43.5, 78, 8, 9, 34.5, 10, 95},
        {75, 44, 40, 29, 39, 5, 99.5},
        {18, 86, 68, 92, 10.5, 11, 4}
    };

    for( int Top=0; Top+2<ROWS; ++Top )
    {
        float Sum = SumOf3Elem(M[Top]) + SumOf3Elem(M[Top+1]) + SumOf3Elem(M[Top+2]);
        printf( "%f ", Sum/9.0f );

        for( int Left=0; Left+3<COLS; ++Left )
        {
            for( int i=0; i<3; ++i )
            {   Sum = Add4th_Sub1st( Sum, M[Top+i]+Left );  }

            printf( "%f ", Sum/9.0f );
        }
        printf( "\n" );
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM