Note that there are some explanatory texts on larger screens.

plurals
  1. POArray diagonal neighbourhood analysis and sort
    primarykey
    data
    text
    <p>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.</p> <p>So far I have methods for extracting the diagonal neighbours:</p> <pre><code> //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; } </code></pre> <p>I've then used this method in an iteration to get diagonals for each value in the original array:</p> <pre><code> //get diagonals for each double [] [] bubbles(){ double [] [] datap = new double [298] [298]; for (int i = 1; i &lt; data.length; i++){ for (int j = 1; j &lt; data[i].length; j++) { if ((i &gt; 0) &amp;&amp; (j &gt; 0)) { if ((i &lt; data.length-1) &amp;&amp; (j &lt; data.length-1)){ double [] tempArray = getDiagonals(i, j); //do something with the tempArray </code></pre> <p>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. </p> <p>I've tried calling other methods into the bubbles() method in order to do all the processing there and return a new array:</p> <pre><code> //get diagonals for each double [] [] bubbles(){ double [] [] datap = new double [298] [298]; for (int i = 1; i &lt; data.length; i++){ for (int j = 1; j &lt; data[i].length; j++) { if ((i &gt; 0) &amp;&amp; (j &gt; 0)) { if ((i &lt; data.length-1) &amp;&amp; (j &lt; data.length-1)){ double [] tempArray = getDiagonals(i, j); double sorted [] = sort(tempArray); double median = sorted[2]; for (int z = 0; z &lt; datap.length; z++){ for (int y = 0; y &lt; datap[z].length; y++){ datap[z][y] = median; } } } } } } return datap; } </code></pre> <p>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 </p> <p>I guess my question is how to process within a method that is iterating and populate a new array?</p> <p>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.</p> <p>Any help would be greatly appreciated (and I'll even reference you if I need to use some code you provide ;) </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload