1

我很难想出一种在二维数组中找到相邻值之和的好方法。我需要在索引位置找到所有相邻值的总和。如果位置是边缘,我需要将它的值转换为总和。因此,每个元素将有 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;
}
4

5 回答 5

1

这是你的解决方案。我知道有很多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];
            }
        }
    }
于 2013-09-10T08:46:36.007 回答
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;
    }**
于 2020-12-23T12:21:31.390 回答
0

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

    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);
        }
    }
于 2013-09-10T10:47:07.833 回答
0

您在该行中收到 ArrayOutOfBounds 异常:

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

因为您正在到达map[i].lengthfor 循环中的最后一个位置并访问下一个(越界)位置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)){
于 2013-09-10T08:24:28.423 回答
0

算法很简单

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;
            }
        }
于 2016-12-27T00:50:12.993 回答