区域中的最大元素 [英] Maximum element in a region

查看:162
本文介绍了区域中的最大元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较数组中元素的值和相邻元素(3x3),找到最大值(图)。我的代码下面似乎不工作。哪一部分出了错?
>

I want to compare the value of an element in an array with the neighboring elements (3x3) and find the maximum (figure). My code below doesn't seem to work. Which part has gone wrong?

int data[10][10] = {};
int dataMax[10][10] = {};
int r_size = 3;  // Size of region to compare
int h = floor(r_size/2);    

for(int i = h; i < ( 10 - h  ) ; i++){       
    for(int j = h; j < ( 10 - h ); j++){
      int max = 0;

        for( int ii = i - h; ii < ( i + h ); ii++ ){
            for( int jj = j - h; jj < ( j + h  ); jj++ ){

               if( data[ii][jj] > max ){

                 max = data[ii][jj];

               }else{

                  dataMax[ii][jj] = 0;

                }
            }
        }

   dataMax[ii][jj]  = data[ii][jj];

    }
}

答案]

int data[10][10] = 
{{0,0,3,0,1,0,0,0,0,1},
{0,0,0,2,0,0,0,0,0,0},
{0,0,0,0,0,0,1,1,0,0},
{0,0,0,0,0,0,0,3,0,0},
{0,4,2,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,8,1,1,0,0},
{0,0,0,0,3,6,0,0,2,0},
{2,0,0,0,0,0,0,0,0,0}};

int dataMax[10][10] = {};
int r_size = 3;  // Size of region to compare
int h = floor(r_size/2.0);    

for(int i = h; i < ( 10 - h  ) ; i++){        //**** correction ****
   for(int j = h; j < ( 10 - h ); j++){      //**** correction ****
     int max = 0;
     int max_x = 0;              //**** correction ****
     int max_y = 0;

    for( int ii = i - h; ii <= ( i + h ); ii++ ){         //**** correction ****     
        for( int jj = j - h; jj <= ( j + h  ); jj++ ){    //**** correction ****

           if( data[ii][jj] > max ){

             max = data[ii][jj];
             max_x = ii;              //**** correction ****
             max_y = jj;

           }else{

   //           dataMax[ii][jj] = 0;

            }
        }
    }

   dataMax[max_x][max_y]  = max;       //**** correction ****

   }
}


$ b b

我希望得到以下dataMax:

I expect to get following dataMax:

推荐答案

以下可能有助于:( https:// ideone.com/xJXzp8

int getMax(int minx, int maxx, int miny, int maxy)
{
    minx = std::max(minx, 0);
    maxx = std::min(maxx, 10);
    miny = std::max(miny, 0);
    maxy = std::min(maxy, 10);
    int res = data[minx][miny];

    for (int x = minx; x != maxx; ++x) {
        for (int y = miny; y != maxy; ++y) {
            res = std::max(res, data[x][y]);
        }
    }
    return res;
}

void compute()
{
    const int h = 3;  // Size of region to compare

    for (int x = 0; x != 10; ++x) {
        for (int y = 0; y != 10; ++y) {
            dataMax[x][y] = getMax(x - h / 2, x + h - h / 2,
                                   y - h / 2, y + h - h / 2);
        }
    }
}

void filter()
{
    for (int x = 0; x != 10; ++x) {
        for (int y = 0; y != 10; ++y) {
            if (dataMax[x][y] != data[x][y]) {
                dataMax[x][y] = 0;
            }
        }
    }
}

这篇关于区域中的最大元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆