Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed a Ruby way to determine the elements of a matrix "touching" another element
    text
    copied!<p>I think I need a method called “Touching” (as in contiguous, not emotional.) I need to identify those elements of a matrix that are next to an individual element or set of elements. At least that’s the way I’ve thought of to solve the problem at hand.</p> <p>The matrix State in the program below represents, let’s say, some underwater topography. As I lower the water, eventually the highest point will stick out and become an “island”. When the “water level” is at 34 then the element State[2,3] is the single point of the island. The array atlantis holds the coordinates of that single point .</p> <p>As we lower the water level further, additional points will be “above water.” Additional contiguous points will become part of the island and their coordinates would be added to the array atlantis. (For example, the next piece of land to be part of atlantis would be State[3,4] at 31.)</p> <p>My thought about how to do this is to identify all the matrix elements that touch/are next to the element in the atlantis, find the one with the highest elevation and then add it to the array atlantis. Looking for the elements next to a single element is a challenge in itself, but we could write some code to examine the set [i,j-1], [i,j+1], [i-1,j-1], [i-1,j], [i-1,j+1], [i+1,j-1], [i+1,J], [i+1,j+1]. (I think I got that right.)</p> <p>But as we add additional points, the task of determining which points surround the points in atlantis becomes increasingly difficult. So that’s my question: can anyone think of any mechanism by which to do this? Any kind of simplified algorithm using capabilities of ruby of which I am unaware? (which include all but the most basic.) If such a method could be written then I could write atlantis.touching and get an array, for example, containing all the coordinates of all the points presently contiguous to atlantis.</p> <p>At least that’s how I’m thinking this could be done. Any other ideas would be welcome. And if anyone knows any kind of partnering site where I could seek others who might be interested in working with me on this, that would be great.</p> <pre><code># create State database using matrix require 'matrix' State=Matrix[ [3,1,4,4,6,2,8,12,8,2], [6,2,4,13,25,21,11,22,9,3,], [6,20,27,34,22,14,12,11,2,5], [6,28,17,23,31,18,11,9,18,12], [9,18,11,13,8,9,10,14,24,11], [3,9,7,16,9,12,28,24,29,21], [5,8,4,7,17,14,19,30,33,4], [7,17,23,9,5,9,22,21,12,21,], [7,14,25,22,16,10,19,15,12,11], [5,16,7,3,6,3,9,8,1,5] ] #find sate elements contiguous to island atlantis=[[2,3]] find all state[i,j] "touching" atlantis </code></pre>
 

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