简体   繁体   English

使用每个元素的邻居的 2D 数组中每个元素的中值

[英]Median of every element on a 2D array using the neighbors of each element

I've been trying my best to solve this on my own, but I have been unable to and I am stuck.我一直在尽力自己解决这个问题,但我一直无法解决,我被困住了。 I feel like this would be very simple if I did not have to consider every element's neighbor.如果我不必考虑每个元素的邻居,我觉得这将非常简单。 What do I mean by that?我的意思是什么? If the case is that I have an element on a corner where in theory it would only have 3 neighbors, as per the instructions on the assignments, I have to use the “missing neighbors” as 0. So for example;如果情况是我在一个角落有一个元素,理论上它只有 3 个邻居,根据作业的说明,我必须使用“缺失的邻居”作为 0。例如;

If I have the 2D Array array2D = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};如果我有二维数组array2D = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

Which could be seen as这可以看作是

1 2 3
4 5 6
7 8 9

If I want to calculate the median of each element, I need to calculate as if neighbors did exist just that as if they were imaginary 0s.如果我想计算每个元素的中位数,我需要计算邻居是否确实存在,就好像它们是虚构的 0 一样。

As if it looked like this好像它看起来像这样

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

So, using the element 1 as an example, If I were to calculate the median of the element I would have to calculate it using 0, 0, 0, 0, 1, 2, 0, 4, 5因此,以元素 1 为例,如果我要计算元素的中位数,我将不得不使用0, 0, 0, 0, 1, 2, 0, 4, 5来计算它

I really have tried everything that comes to mind but I am not being able to get this to work and I have tried everything I have found.我真的尝试了所有想到的东西,但我无法让它发挥作用,我已经尝试了我发现的所有东西。

Could I please get some help to see if I can get this done?我可以请一些帮助,看看我能不能完成这项工作?

I was able to do this我能够做到这一点

public static double[][] media(double[][] X)
    {
        int numRows = X.length;
        int numCols = X[0].length;
        double[][] arrayMedian = new double[numRows][numCols];
        for(int row = 0; row < numRows; row++) {
            for(int col = 0; col < numCols; col++) {
                for (int i = Math.max(0, row -1); i < Math.min(numRows, row + 2); i++) {
                    for (int j = Math.max(0, col -1); j < Math.min(numCols, col + 2); j++) {
                        //do stuff

But that only takes the numbers on the actual 2D array and I am not sure how to go about implementing the 0s但这只需要实际二维数组上的数字,我不确定如何实现 0

PS Main has the list there thus why it is not on the code above PS Main 那里有列表,因此为什么它不在上面的代码中

List on main: double[][] X = {{1,2,3}, {4,5,6}, {7,8,9}};主列表: double[][] X = {{1,2,3}, {4,5,6}, {7,8,9}};

I think it would be better to break down the problem in: computing the median for a specific (i;j) value and creating a matrix filled with the medians of the input values.我认为最好将问题分解为:计算特定 (i;j) 值的中值并创建一个填充了输入值中值的矩阵。 This would bring us to define two methods:这将使我们定义两种方法:

  • double[][] getMedianMatrix(doubel[][] matrix)
  • double median(double[][] matrix, int row, int col)

The median method could return the median from a (i;j) value of the given matrix and then check its bounds by computing the rows above and below and the columns on its left and right. median方法可以从给定矩阵的 (i;j) 值返回中值,然后通过计算上下行及其左右列来检查其边界。 It could break down the problem in three macro cases where each row is handled ( rowAbove , row , and rowBelow ) and for each row check the columns bounds.它可以在处理每一行( rowAboverowrowBelow )并为每一行检查列边界的三个宏案例中分解问题。

The getMedianMatrix method would simply return a matrix with the same dimensions as the input matrix and then invoke the median() method on each element. getMedianMatrix方法将简单地返回一个与输入矩阵具有相同维度的矩阵,然后在每个元素上调用median()方法。

public static double median(double[][] matrix, int row, int col) {
    //Making sure that the given indexes have proper values
    if (row < 0 || row > matrix.length || col < 0 || col > matrix[row].length) {
        return 0;
    }

    //List of values to compute the median with
    List<Double> listNeighbors = new ArrayList<>();

    //Defining the neighbor coordinates
    int newRowAbove = row - 1, newRowBelow = row + 1, newColLeft = col - 1, newColRight = col + 1;

    //Checking if exists a row above the current index, if it does then each column is checked otherwise 3 zeros are added to the list
    if (newRowAbove >= 0) {
        listNeighbors.add(newColLeft >= 0 ? matrix[newRowAbove][newColLeft] : 0.0);
        listNeighbors.add(matrix[newRowAbove][col]);
        listNeighbors.add(newColRight < matrix.length ? matrix[newRowAbove][newColRight] : 0.0);
    } else {
        listNeighbors.add(0.0);
        listNeighbors.add(0.0);
        listNeighbors.add(0.0);
    }

    //Adding the current row elements making sure that the adjacent columns are consistent
    listNeighbors.add(newColLeft >= 0 ? matrix[row][newColLeft] : 0.0);
    listNeighbors.add(matrix[row][col]);
    listNeighbors.add(newColRight < matrix.length ? matrix[row][newColRight] : 0.0);

    //Checking if exists a row below the current index, if it does then each column is checked otherwise 3 zeros are added to the list
    if (newRowBelow < matrix.length) {
        listNeighbors.add(newColLeft >= 0 ? matrix[newRowBelow][newColLeft] : 0.0);
        listNeighbors.add(matrix[newRowBelow][col]);
        listNeighbors.add(newColRight < matrix.length ? matrix[newRowBelow][newColRight] : 0.0);
    } else {
        listNeighbors.add(0.0);
        listNeighbors.add(0.0);
        listNeighbors.add(0.0);
    }

    //Sorting the elements by their natural ordering
    listNeighbors.sort(Comparator.naturalOrder());

    //Since we always have an add number of elements (9) the median is given by the middle element
    return listNeighbors.get(listNeighbors.size() / 2);
}

Here is a link to test the code:这是测试代码的链接:

https://www.jdoodle.com/iembed/v0/s65 https://www.jdoodle.com/iembed/v0/s65

Output输出

 0,0  2,0  0,0 
 2,0  5,0  3,0 
 0,0  5,0  0,0 

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

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