【问题标题】:2d array. Finding the sum of neighbor values二维数组。查找相邻值的总和
【发布时间】:2013-09-13 20:37:15
【问题描述】:

我很难想出一种在二维数组中找到相邻值之和的好方法。我需要在索引位置找到所有相邻值的总和。如果位置是边缘,我需要将它的值转换为总和。因此,每个元素将有 4 个值对其总和做出贡献。

示例。

索引[0][0] 处的元素 = 5.0。 [0][1] = 2,并且 [1][0] = 4。 index[0][0] = 5(左/自身镜像)+ 5(上/自身镜像)+ 2(右侧)+ 4(下方)= 16。

到目前为止,我的代码如下。更好的方法的建议将不胜感激。这段代码得到了一个

ArrayOutOfBounds: 3

在注释后面的行上 //ERROR**

Public static double [][] getSumWeight(double [][] map){
    int base = 0;
    //int count = getEnhancementLevel();
    int row = map.length;
    int col = map[0].length;

    int newRow = row*2;
    int newCol = col*2;

    double [] [] sumMap = new double [row][col];
    double [][] weightMap = new double [newRow][newCol];
    //getting the corner sum weights
        for (int i = 0; i < map.length; i++){
            for (int j = 0; j < map[i].length; j++){
                if (i == 0 && j == 0){
                    double result = 0;
                    result += (map[0][0])*2; 
                    result += map[0][1];
                    result += map[1][0];
                    sumMap[0][0] = result;
                    System.out.println("corner" + sumMap[0][0]);
                }
                else if (i == 0 && j == map[0].length){
                    double result = 0;
                    result += map[0][map[0].length];
                    result += map[0][map[0].length-1];
                    result += map[1][map[0].length];
                    sumMap[i][j] = result;

                }else if (i == map.length && j == 0){

                }else if (i == map.length && j == map[map.length].length){

                }
                //getting the mid(s) of the top row
                else if (i == 0 && j != 0 && j != map[0].length){
                    double result = 0;
                    result += map[i][j]; //top value or mirror
                    result += map[i][j-1]; // left value

                    //ERROR****
                    result += map[i][j+1]; // right value
                    result += map[i+1][j]; // bottom value
                    sumMap[i][j] = result;

                }
                //getting the mid(s) of the bottom row
                else if (i == map.length && j != 0 && j != map[map.length].length){
                    double result = 0;
                    result += map[i][j];
                    result += map[i][j-1];
                    result += map[i][j+1];
                    result += map[i-1][j];
                    sumMap[i][j] = result;
                }
                //getting the mid(s) of the left most column
                else if (j == 0 && i != 0 && i != map.length){
                    double result = 0;
                    result += map[i][j];
                    result += map[i-1][j];
                    result += map[i+1][j];
                    result += map[i][j+1];
                    sumMap[i][j] = result;
                }
                //getting the mid(s) of the right most column
                else if (j == map[0].length && i != 0 && i != map.length){
                    double result = 0;
                    result += map[i][j];
                    result += map[i-1][j];
                    result += map[i+1][j];
                    result += map[i][j-1];
                    sumMap[i][j] = result;
                }
                //filling in all the remaining values
                else{
                    double result = 0;
                    result += map[i-1][j];
                    result += map[i+1][j];
                    result += map[i][j-1];
                    result += map[i][j+1];
                    sumMap[i][j] = result;
                }
        }



}
        for (int i = 0; i < map.length; i++){
            for (int j = 0; j < map[i].length; j++){
                System.out.println(sumMap[i][j]);
        }}
        return sumMap;
}

【问题讨论】:

    标签: java arrays sum type-2-dimension


    【解决方案1】:
     **//The function receives a matrix and replaces each value in the matrix with the value of the sum of its neighbors 
        public static int[][] sumOfNeighbours(int[][] mat) {
            int[][] sum = new int[mat.length][mat[0].length];
            for (int i = 0; i < mat.length; i++) {
                for (int j = 0; j < mat[i].length; j++) {
                    if (i == 0) {
                        if (j == 0) {
                            sum[i][j] = mat[i + 1][j + 1] + mat[i][j + 1] + mat[i + 1][j];//dells with the top left corner
                        } else if (j == mat[i].length - 1) {
                            sum[i][j] = mat[i + 1][j - 1] + mat[i][j - 1] + mat[i + 1][j];//dells with the top right corner
                        } else {
                            sum[i][j] = mat[i][j - 1] + mat[i][j + 1] + mat[i + 1][j + 1] + mat[i + 1][j - 1] + mat[i + 1][j];//top middles
                        }
                    } else if (i == mat.length - 1) {
                        if (j == 0) {
                            sum[i][j] = mat[i - 1][j] + mat[i - 1][j + 1] + mat[i][j + 1];//dells with the bottom left corner
                        } else if (j == mat[i].length - 1) {
                            sum[i][j] = mat[i - 1][j] + mat[i][j - 1] + mat[i - 1][j - 1];//dells with the bottom right corner
                        } else {
                            sum[i][j] = mat[i - 1][j] + mat[i - 1][j - 1] + mat[i - 1][j + 1] + mat[i][j + 1] + mat[i][j - 1];//dells with the bottom middles
                        }
                    } else if (j == 0) {//first column
                        if ((i != 0) && (i != mat.length - 1))
                            sum[i][j] = mat[i - 1][j] + mat[i + 1][j] + mat[i + 1][j + 1] + mat[i - 1][j + 1] + mat[i][j + 1];//dells with the left middles
                    } else if (j == mat[i].length - 1) {//last column
                        if ((i != 0) && (i != mat.length - 1))
                            sum[i][j] = mat[i - 1][j] + mat[i + 1][j] + mat[i - 1][j - 1] + mat[i][j - 1] + mat[i + 1][j - 1];//dells with the right  middles
                    } else {
                        sum[i][j] = mat[i][j - 1] + mat[i][j + 1] + mat[i + 1][j] + mat[i + 1][j - 1] + mat[i + 1][j + 1] + mat[i - 1][j] + mat[i - 1][j - 1] + mat[i - 1][j + 1];//dells with the all the rest
                    }
                }
    
            }
    
            return sum;
        }**
    

    【讨论】:

      【解决方案2】:

      算法很简单

      int sum[][] = new int[c][c];
      for(int i = 0; i < arr.length; i++)
      {
              for(int j = 0; j < arr[i].length; j++)
                  {
                      int result = 0;
      
                      if(j > 0)
                          result += arr[i][j-1];
                      if(j < arr[i].length - 1)
                          result += arr[i][j+1];
                      if(i > 0)
                          result += arr[i-1][j];
      
                      if(i > 0 && j > 0)
                          result += arr[i-1][j-1];
                      if(i > 0 && j < arr[i].length - 1)
                          result += arr[i-1][j+1];
      
                      if(j > 0 && i < arr.length -1)
                          result += arr[i+1][j-1];
                      if(j < arr[i].length - 1 && i < arr.length -1)
                          result += arr[i+1][j+1];
                      if(i < arr.length -1)
                          result += arr[i+1][j];
                      sum[i][j] = result;
                  }
              }
      

      【讨论】:

        【解决方案3】:

        算法要简单得多(我更改了第一个解决方案,因为我第一眼就理解了需求错误的需求);所有的逻辑都在第一个。我主要摆脱了所有不需要的循环......

            public class MatrixAlgoritm {
        
        
                public static double[][] getSumWeight(double[][] map) {
                    int row = map.length;
                    int col = map[0].length;
        
                    double[][] sumMap = new double[row][col];
        
                    for (int i = 0; i < map.length; i++) {
                        for (int j = 0; j < map[i].length; j++) {
                            double result = 0;
                            result += map[Math.max(0, i - 1)][j];
                            if (i == map.length - 1) {
                                result += map[i][j];
                            } else {
                                result += map[i + 1][j];
                            }
                            result += map[i][Math.max(0, j - 1)];
                            if (j == map[0].length - 1) {
                                result += map[i][j];
                            } else {
                                result += map[i][j + 1];
                            }
                            sumMap[i][j] = result;
                        }        
                    }
                    for (int i = 0; i < row; i++) {
                        for (int j = 0; j < col; j++) {
                            System.out.println(sumMap[i][j]);
                        }
                    }
                    return sumMap;
                }
        
        
                public static void main(String[] args) {
                    double[][] baseMap = {{2, 10, 7}, {4, 5, 8}, {5, 6, 9}};
                    getSumWeight(baseMap);
                }
            }
        

        【讨论】:

        • 这很接近,但值在某处被错误地求和。示例:double [][] baseMap = {{2, 10, 7}, {4, 5, 8}, {5, 6, 9}};输出为 val: 28.0 val: 19.0 val: 36.0 val: 17.0 val: 28.0 val: 26.0 val: 20.0 val: 24.0 val: 28.0
        • 应该是 sumMap = {{18, 24, 32}, {16, 28, 29}, {20, 25, 32}};
        • @Keystone1722 为什么?在您的示例中,元素 (0,0) 为 2。因此计算结果的总和是 4+4+10+10 = 28 等等。这是我的结果。我错过了一个要求吗?可以复查吗?
        • 我终于明白了要求并修正了计算。我认为更干净。
        【解决方案4】:

        这是您的解决方案。我知道有很多 if else 声明,但这是我最好的。

        for(int i=0;i<a.length;i++){
                for(int j=0;j<a[i].length;j++){
                    if(i==0){
                        if(j==0){
                            sum[i][j] = a[i][j]*2 + a[i+1][j] + a[i][j+1];
                        }
                        else if(j==a[i].length-1){
                            sum[i][j] = a[i][j]*2 + a[i][j-1] + a[i+1][j] ;
                        }
                        else{
                            sum[i][j] = a[i][j] + a[i][j-1] + a[i+1][j] + a[i][j+1];
                        }
                    }
                    else if(i==a.length-1){
                        if(j==0){
                            sum[i][j] = a[i][j]*2 + a[i-1][j] + a[i][j+1];
                        }
                        else if(j==a[i].length-1){
                            sum[i][j] = a[i][j]*2 + a[i][j-1] + a[i-1][j] ;
                        }
                        else{
                            sum[i][j] = a[i][j] + a[i][j-1] + a[i-1][j] + a[i][j+1];
                        }
                    }
                    else if(j==0){
                        sum[i][j] = a[i][j] + a[i-1][j] + a[i+1][j] + a[i][j+1];
                    }
                    else if(j==a[i].length-1){
                        sum[i][j] = a[i][j] + a[i-1][j] + a[i+1][j] + a[i][j-1];
                    }
                    else{
                        sum[i][j] = a[i][j-1] + a[i-1][j] + a[i+1][j] + a[i][j+1];
                    }
                }
            }
        

        【讨论】:

        • 这很接近,但值在某处被错误地求和。示例:double [][] baseMap = {{2, 10, 7}, {4, 5, 8}, {5, 6, 9}};输出为 val: 0.0 val: 0.0 val: 7.0 val: 0.0 val: 0.0 val: 8.0 val: 5.0 val: 6.0 val: 9.0
        • 何时应该是 {{18, 24, 32}, {16, 28, 29}, {20, 25, 32}};
        • @Keystone1722 我得到了正确答案。告诉我你在做什么?并再次检查变量的类型。
        • 你是对的。对于您的 [][] 'a',我使用的是 sum[][]。非常感谢!
        【解决方案5】:

        您在该行中收到 ArrayOutOfBounds 异常:

        result += map[i][j+1]; // right value
        

        因为您到达了 for 循环中的最后一个位置 map[i].length 并访问了下一个(越界)位置 map[i][j+1]

        请注意,您正在检查您是否错误地到达了最后一个位置:

        Example: map[0].length = 4 (0,1,2,3)
        
        else if (i == 0 && j != 0 && j != map[0].length){ // j=3 but j!=4
             result += map[i][j+1] //<- error
        }
        

        你应该检查:

        else if (i == 0 && j != 0 && j != (map[0].length -1)){
        

        【讨论】:

        • 不,他想要i == map.length - 1 进行所有检查,我认为他仍想遍历最后一行。
        • 是的。我需要遍历所有行和列并用总和构建一个新数组。
        • 已更改。但现在它又抛出了另一个越界:'else' 语句的第一个结果 += -1。 if, if else, else 块中的最后一条语句
        • 现在你在 i=0 时访问 -1 位置
        猜你喜欢
        • 2017-10-04
        • 1970-01-01
        • 2013-03-16
        • 2017-03-12
        • 1970-01-01
        • 2010-10-13
        • 2015-12-13
        • 2016-08-26
        相关资源
        最近更新 更多