2d array. Finding the sum of neighbor values

user2541019 picture user2541019 · Sep 10, 2013 · Viewed 9k times · Source

I am having a hard time coming up with a good way of finding the sum of the neighbouring values in a 2d array. I need to find the sum of all neighbouring values at an index position. If the position is an edge, I need to transpose it's value into the sum. So every element will have 4 values contributing to its sum.

Example.

Element at index[0][0] = 5.0. [0][1] = 2, and [1][0] = 4. The sum of index[0][0] = 5(left/mirror of itself) + 5 (up/mirror of itself) + 2(right of it) + 4 (below it) = 16.

My code so far is below. Suggestions for a better way would be appreciated. This code is getting an

ArrayOutOfBounds: 3

on the line after the comment saying //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;
}

Answer

Vimal Bera picture Vimal Bera · Sep 10, 2013

This is your solution. I know there are lots of if else statements but this is my best.

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];
            }
        }
    }