-1

假设我有一个数组数组:

double[][] img = new double[row][col];

我想在一个 2x2 块中循环 img ......例如:

2,  4, 31, 31   
3,  3, 21, 41
1,  2, 10, 20
3,  2, 20, 30

然后您首先查看第一个 2x2 子数组(从左上角开始)

2,  4
3,  3

然后我们看下一个 2x2 块

31,  31
21,  41

其他块将是 1,2,3,2 和 10,20,20,30...

如何制作一个循环,让它像这样通过?本质上,我这样做是为了找到块中值的平均值,并用该平均值替换数组中的每个元素。

4

3 回答 3

3

You will need two nested for loops. But unlike normal for loops, instead of incrementing your looping variables, in both cases, add 2 to your index. Then, inside the inner for loop, assuming you have looping indexes i and j, refer to your 4 values with img[i][j], img[i + 1][j], img[i][j + 1], and img[i + 1][j + 1]. But you'll have to be careful if row or col is odd.

于 2013-03-01T20:26:59.823 回答
2

You could use a structure like this:

double[][] img = new double[row][col];
//This will break if row or col are odd, make sure you are always passing an even amount or check for this case.
for (int i = 0; i < row; i+=2) {
    for (int j = 0; j < col; j+=2) {
        //Do what you need with these values:
        img[i][j];     //Top left
        img[i+1][j];   //Top right
        img[i][j+1];   //Bottom left
        img[i+1][j+1]; //Bottom right
    }
}
于 2013-03-01T20:27:22.763 回答
1

可能对您有帮助:

tile = 2;
for(i = 0; i < row; i = tile + i)
 for(j = 0; j < col; j= tile + j)
  for(r = 0; r < tile; r++)   
    for(c = 0; c < tile; c++) 
      System.out.print(" " + img[i+r][j+c]);
    System.out.print("\n");

如果您需要其他尺寸,请放置tile尺寸,然后 2*2:

编辑
现在我提供完整的代码。

class BreakWithLabelDemo {
    public static void main(String[] args) {

        int[][] img = { 
                    {55, 60, 65, 1},
                    {95, 90, 85, 5},
                    {5,  0,  8,  5},  
                    {53, 60, 89, -5}
        };


        int tile=2; 
        int row=4; 
        int col=4;
        int i, r;
        int j, c;

        tile = 2;
        for(i = 0; i < row; i= tile + i)
           for(j = 0; j < col; j= tile + j){
              for(r = 0; r < tile; r++){   
                 for(c = 0; c < tile; c++) 
                    System.out.print(" ", img[i+r][j+c]);
                 System.out.println("");
              }
              System.out.println("\n");
           }



        int img2[][] = { 
                  // 1   2   3   4  5  6  7  8  9 
                    {55, 60, 65, 1, 2, 4, 1, 4, 0},
                    {95, 90, 85, 5, 3, 6, 5, 0, 8},
                    {5,  0,  8,  5,-1, 2, 2, 5, 6},  
                    {95, 90, 85, 5, 3, 6, 5, 0, 8},
                    {55, 60, 65, 1, 2, 4, 1, 4, 0},
                    {5,  0,  8,  5,-1, 2, 2, 5, 6},  
                    {1,  2,  3,  4, 5, 6, 7, 8, 9}, 
                    {1,  2,  3,  4, 5, 6, 7, 8, 9}, 
                    {1,  2,  3,  4, 5, 6, 7, 8, 9}
                  };

         row = 9; 
         col = 9;
         tile = 3;

         for(i = 0; i < row; i= tile + i)
           for(j = 0; j < col; j= tile + j){
              for(r = 0; r < tile; r++){   
                 for(c = 0; c < tile; c++) 
                    System.out.print(" ", img[i+r][j+c]);
                 System.out.println("");
              }
              System.out.println("\n");
           }
    }
}

这实际上是有效的:

正在运行的实例

 55  60 
 95  90 


 65   1 
 85   5 


  5   0 
 53  60 


  8   5 
 89  -5 


 55  60  65 
 95  90  85 
  5   0   8 


  1   2   4 
  5   3   6 
  5  -1   2 


  1   4   0 
  5   0   8 
  2   5   6 


 95  90  85 
 55  60  65 
  5   0   8 


  5   3   6 
  1   2   4 
  5  -1   2 


  5   0   8 
  1   4   0 
  2   5   6 


  1   2   3 
  1   2   3 
  1   2   3 


  4   5   6 
  4   5   6 
  4   5   6 


  7   8   9 
  7   8   9 
  7   8   9 
于 2013-03-01T20:26:52.137 回答