Question

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;
}
Était-ce utile?

La solution

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

Autres conseils

 **//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;
    }**

You're getting an ArrayOutOfBounds exception in the line:

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

because you're reaching the last position map[i].length in the for loop and accessing the next (out of bounds) position map[i][j+1];

Notice you are checking you've reached the last position incorrectly:

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
}

You should check:

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

The algoritm is much more simple (I changed the first solution as I understood the requirement wrong the requirements at a first glance); all the logic is in the first for. I got rid of mainly all the unrequired loops.....

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

The algorithm is pretty simple

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;
            }
        }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top