简体   繁体   中英

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;

If I have the 2D Array 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.

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

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

PS Main has the list there thus why it is not on the code above

List on main: 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. 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. 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.

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.

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

Output

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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