Array diagonal neighbourhood analysis and sort

1k views Asked by At

I've been battling with this for some time and seem to be getting nowhere. The set up is so; I have a 2D array. For this array I need to iterate through each value and return the diagonal neighbours (5 values). These neighbours will be put into a new 1D [5] array and bubblesorted. The middle value (median) will then be returned and put into a new array of medians.

So far I have methods for extracting the diagonal neighbours:

    //get diagonals from original DEM

    double [] getDiagonals(int i, int j) {

        double [] tempArray = new double [5];

        tempArray[0] = data[i -1][j +1];
        tempArray[1] = data[i -1][j -1];
        tempArray[2] = data[i][j];
        tempArray[3] = data[i +1][j -1];
        tempArray[4] = data[i +1][j +1];


        return tempArray;
    }

I've then used this method in an iteration to get diagonals for each value in the original array:

        //get diagonals for each 

    double [] []   bubbles(){

        double [] [] datap = new double [298] [298];

        for (int i = 1; i < data.length; i++){
            for (int j = 1; j < data[i].length; j++) {
                if ((i > 0) && (j > 0)) {
                    if ((i < data.length-1) && (j  < data.length-1)){

                         double [] tempArray = getDiagonals(i, j);
//do something with the tempArray

I think this is where I'm coming unstuck. Through testing the getDiagonals method works fine. I'm struggling to get the tempArray out of the bubbles() method. If I set the output to be the tempArray it only returns the 5 values calculated for the bottom right corner of the original array.

I've tried calling other methods into the bubbles() method in order to do all the processing there and return a new array:

    //get diagonals for each 

    double [] []   bubbles(){

        double [] [] datap = new double [298] [298];

        for (int i = 1; i < data.length; i++){
            for (int j = 1; j < data[i].length; j++) {
                if ((i > 0) && (j > 0)) {
                    if ((i < data.length-1) && (j  < data.length-1)){

                         double [] tempArray = getDiagonals(i, j);
                         double sorted [] = sort(tempArray);
                         double median = sorted[2];


                            for (int z = 0; z < datap.length; z++){
                                for (int y = 0; y < datap[z].length; y++){
                                datap[z][y] = median;
                                }
                            }   



                    }
                }   
            }
        }
        return datap;
    }

Again this fails and the output datap is just zeros. The sort() method above passed out the diagonals to a bubble sort method (which I know works on its

I guess my question is how to process within a method that is iterating and populate a new array?

I hope this makes sense but if you need more details please let me know. And yes, the sort I'm using is a bubble sort. I know they are rubbish but this is for a course I'm doing so it has to be used. And yes, I'm pretty new to java.

Any help would be greatly appreciated (and I'll even reference you if I need to use some code you provide ;)

2

There are 2 answers

0
Oliver Burdekin On BEST ANSWER

Finally cracked it. To populate the whole array the following code works a peach.

//Diagonal to 1dArray and sorting

double [] [] bubbles()
{
    double [][] tempArray = new double [300][300];

    int y = 0;
    int z = 0;
    double median = 0;

    for (int i = 0; i < data.length; i++)
    {
        for (int j = 0; j < data[i].length; j++)
        {
            if ((i > 0) && (j > 0))
            {
                if ((i +1 < data[i].length) && (j +1 < data[j].length))
                {
                    double [] diagonals = getDiagonals(i, j);

                    //Need to sort here
                    median = diagonals[2];
                    tempArray[i][j] = median;
                }
            }
        }
    }
    return tempArray;
}

The sorting was taken out and I haven't tested with it back in yet; but so far this is providing new values for all the cells in the temp array.

1
Hari Seldon On

The main issue that I see, is that with each traversal through your inner loop of:

for (int i = 1; i < data.length; i++){             
   for (int j = 1; j < data[i].length; j++) {

Where you call:

double [] tempArray = getDiagonals(i, j);

You are resetting all of the values of datap to be the current calculated median. To fix, you would need some way to indicate only the indices of the particular datap value that you want to populate.

You need to replace this section of your code:

for (int z = 0; z < datap.length; z++){
    for (int y = 0; y < datap[z].length; y++){
    datap[z][y] = median;
    }
}   

You could declare int y, z at the top of the method and do something like this:

if (y < datap.length){
    if (z == datap.length[y] - 1){
        y++;
        z = 0;
    }

    datap[y][z] = median;
    z++;
}

That way you are only assigning to the specific index in datap that you are trying to reach, instead of resetting each of its values.