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
P.S 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}};