繁体   English   中英

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

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

我一直在尽力自己解决这个问题,但我一直无法解决,我被困住了。 如果我不必考虑每个元素的邻居,我觉得这将非常简单。 我的意思是什么? 如果情况是我在一个角落有一个元素,理论上它只有 3 个邻居,根据作业的说明,我必须使用“缺失的邻居”作为 0。例如;

如果我有二维数组array2D = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

这可以看作是

1 2 3
4 5 6
7 8 9

如果我想计算每个元素的中位数,我需要计算邻居是否确实存在,就好像它们是虚构的 0 一样。

好像它看起来像这样

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

因此,以元素 1 为例,如果我要计算元素的中位数,我将不得不使用0, 0, 0, 0, 1, 2, 0, 4, 5来计算它

我真的尝试了所有想到的东西,但我无法让它发挥作用,我已经尝试了我发现的所有东西。

我可以请一些帮助,看看我能不能完成这项工作?

我能够做到这一点

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

但这只需要实际二维数组上的数字,我不确定如何实现 0

PS Main 那里有列表,因此为什么它不在上面的代码中

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

我认为最好将问题分解为:计算特定 (i;j) 值的中值并创建一个填充了输入值中值的矩阵。 这将使我们定义两种方法:

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

median方法可以从给定矩阵的 (i;j) 值返回中值,然后通过计算上下行及其左右列来检查其边界。 它可以在处理每一行( rowAboverowrowBelow )并为每一行检查列边界的三个宏案例中分解问题。

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

这是测试代码的链接:

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

输出

 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