简体   繁体   English

Java Array - 计算周围数字的平均值

[英]Java Array - Calculate average of surrounding numbers

Ok, so I have an array like so: 好的,所以我有一个像这样的数组:

1 2 3 1 2 3
4 5 6 4 5 6
7 8 9 7 8 9

Doesn't necessarily have to be any specific size, just showing it so you guys have something to look at. 不一定要有任何特定的尺寸,只是显示它,所以你们有一些东西要看。 So I have to go through each number, and calculate the average of the numbers that are surrounding it. 所以我必须遍历每个数字,并计算它周围的数字的平均值。 For example, for the number 1 I have to calculate the average of 2, 5, and 4. I have to compare that average to the number itself, but that's not my issue. 例如,对于数字1,我​​必须计算平均值2,5和4.我必须将该平均值与数字本身进行比较,但这不是我的问题。 My issue is running through an array of any size (determined by the user) and examining the numbers around it. 我的问题是运行任何大小的数组(由用户确定)并检查它周围的数字。 Now, I could program a separate method for each section, meaning a separate method for each corner, a separate method for each side, and a method for the center. 现在,我可以为每个部分编写一个单独的方法,这意味着每个角落都有一个单独的方法,每个角落都有一个单独的方法,以及一个中心的方法。 But that sounds like a lot more work and a lot less efficient. 但这听起来像是更多的工作而且效率低得多。 So, I've decided to make a sort of border around the outside. 所以,我决定在外面做一个边界。 This border will be filled with 0's, assuming that 0 will never be an input value. 假设0永远不是输入值,此边框将填充0。 This is so that I will not run out of my array, but i can run one coded function on the inside of this border, so that it calculates the average of the 8 surrounding numbers. 这样我就不会用完我的数组了,但是我可以在这个边框内部运行一个编码函数,这样它就可以计算出8个周围数字的平均值。

So I'm wondering how I could go about running through an array and ignore any 0's it comes across, but take the average of the other numbers around it? 所以我想知道如何通过数组运行并忽略它遇到的任何0,但是取其周围的其他数字的平均值?

Example: 例:
0 0 0 0 0 0 0 0 0 0
0 1 2 3 0 0 1 2 3 0
0 4 5 6 0 0 4 5 6 0
0 7 8 9 0 0 7 8 9 0
0 0 0 0 0 0 0 0 0 0

so in the first spot, it'd take the average of the numbers 2, 4, and 5, but ignore the 0's. 所以在第一个位置,它取数字2,4和5的平均值,但忽略0。 the reason I need to ignore them is because: 我需要忽略它们的原因是因为:
1. I cannot include them in the number count because even though adding 0 won't affect the total, it'll affect the amount of numbers to divide by in obtaining the average 1.我不能将它们包含在数字计数中,因为即使添加0不会影响总数,它也会影响在获得平均值时除以的数字量。
2. I won't actually be using 0 since 0 is a possible input, it'll actually be -1. 2.我实际上不会使用0,因为0是可能的输入,它实际上是-1。 I put 0 here for the ease of typing and readability. 为了方便打字和可读性,我把0放在这里。

Suggested best method of going about this? 建议最好的方法来解决这个问题?

EDIT2: EDIT2:

So now, I'm getting an output that's close to what I should be getting. 所以现在,我得到的输出接近我应该得到的。 But I've thoroughly looked through and I can't see where this error is coming about. 但是我已经彻底查看了,我无法看到这个错误发生在哪里。

public static int calculate(int[][] array, int row, int col){
    double num = 0;//Used to find the total of real numbers to be divided by
    double total = 0;
    double average;

    for (int a = -1; a <= 1; a++) 
    {
        for (int b = -1; b <= 1; b++) 
        {
            if(row + a < 0 || col + b < 0)
            {
                continue;
            }
            if(a == 0 && b == 0)//if(row + a == row || col + b == col)
            {
                continue;
            }
            if(row + a >= r || col + b >= c)
            {
                continue;
            }
            else
            {                   
                num+=1;
                total+= array[row + a][b + col];
                System.out.println("row+a = " + (row + a) + " and col+b = " + (col + b));
            }
        }
     }

    average = (double)(total/num);
    count++;
    System.out.print(count + ". Average = " + total + " divided by " + num + " = " + average + "\n");

    if (average < array[row][col])
        return 255;
    else
        return 0;
}

Into this function, I put each value in the array (going through each row) to be changed to either 0 or 255. Apparently, the correct final result should be: 0 255 0 255 0 在这个函数中,我把数组中的每个值(通过每一行)改为0或255.显然,正确的最终结果应该是:0 255 0 255 0

0 0 255 0 0 0 0 255 0 0

255 0 255 0 255 255 0 255 0 255

255 0 0 0 0 255 0 0 0 0

0 255 0 0 255 0 255 0 0 255

But I'm getting 但我得到了

0 255 0 255 0 0 255 0 255 0

0 0 0 0 0 0 0 0 0 0

255 0 255 0 255 255 0 255 0 255

0 0 0 0 0 0 0 0 0 0

255 0 255 0 255 255 0 255 0 255

So I'm looking into those specific differences and I'm going to see where the issue is. 所以我正在研究那些具体的差异,我将会看到问题所在。

So, I've decided to make a sort of border around the outside. 所以,我决定在外面做一个边界。

Trivia - Dan and Kathy Spacklen (Spacken?) did this wit their chessboard when they wrote Sargon 30 years ago. 琐事 - 丹和Kathy Spacklen(Spacken?)在30年前写过Sargon的时候用棋盘做了这个。

You can do the border but I think it isn't going to help much. 你可以做边境,但我认为它不会有多大帮助。 Tell me, in your first example 在你的第一个例子中告诉我

1 2 3
4 5 6
7 8 9

Assuming #1 is at [0][0], what would be the indexes for the 8 cells surrounding #2? 假设#1在[0] [0],那么#2周围的8个单元的索引是什么?

It seems to me that the simplest solution is to go through all possible cells in a loop and for each to see if it's inside the matrix. 在我看来,最简单的解决方案是遍历循环中的所有可能单元格,并为每个单元格查看它是否在矩阵内。 It's not exactly short, but there's no ugly unreadable conditions involved and you don't have to surround matrix with anything. 它并不完全是短暂的,但是没有任何丑陋的不可读条件,你不必用任何东西包围矩阵。

int sum = 0;
int amount = 0;
for (int di = -1; di <= 1; ++di) {
    for (int dj = -1; dj <= 1; ++dj) {
        // check that cell (i0 + di, j0 + dj) is inside the matrix
        // and not the original cell (i0, j0)
        // increase 'sum' and 'amount' if necessary
    }
}

return (double) sum / amount;

edit 编辑
Removed part of the code to leave some challenge in the problem 删除了部分代码,在问题中留下了一些挑战

The following is the code that I use to determine if a cell lives or dies in the game of life . 以下是我用来确定一个细胞是否在生命游戏中生存或死亡的代码。

private static boolean lives(Point p, byte[][] squares) {
    int liveNeighboors = 0;
    for (int i = p.x - 1; i < p.x + 2; i++) {
        for (int j = p.y - 1; j < p.y + 2; j++) {
            if (new Point(i, j).equals(p))
                continue;
            try {
                liveNeighboors += squares[i][j];
            } catch (Exception e) {
                // this happens if you try to sum an edge space
            }
        }
    }
    if (squares[p.x][p.y] == 0) {
        return liveNeighboors == 3;
    } else {
        return liveNeighboors == 2 || liveNeighboors == 3;
    }
}

You could adapt this to sum the numbers and return the average instead of whether or not the cell should live or die. 您可以对此进行调整以对数字求和并返回平均值,而不是单元格是否应该存活或死亡。

This would need to be called in a loop like so: 这需要在循环中调用,如下所示:

for(int i = 0;  i < arr.length; i++) {
    for (int j = 0;j < arr[0].length; j++) {
        lives(new Point(i,j), arr);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM