title: How to extract and record values, and their respective column/row information, from a matrix for 3D rendering?
body: <p>I am bringing thermal images into R as 480 X 640 matrices. After isolating my "signal" (seedlings in a field), I would like to <strong>export values to a data.table while also recording row and column of each observation</strong>. I need a final data.table with basically x,y,z information. I am plotting the raw thermal matrices as a 3D surface in <code>rgl</code> and would like to make another 3D layer in my renderings that illustrates my "signal". </p>
  
  <p>So I start with example data as follows: </p>
  
  <pre><code>library(data.table)
  library(rgl)
  
  dd &lt;- data.table(c(1,0,1,2,1,0,2,1,0,2),
                   c(0,1,2,1,3,2,4,1,2,0),
                   c(3,2,1,3,2,4,3,2,1,0),
                   c(2,1,0,2,1,3,2,3,1,1),
                   c(0,1,2,1,3,2,0,1,1,3))
  
  bb &lt;- as.matrix(dd) # save a copy for later
  
  find_signal = function(DT, max, min) {  # inverse of Dowle's NA remover
    for (j in seq_len(ncol(DT)))
      set(DT,which(DT[[j]] &lt; min),j,NA)
    for (j in seq_len(ncol(DT)))
      set(DT,which(DT[[j]] &gt; max),j,NA)
  }
  
  find_signal(dd, 2.0001, 1.9999)
  </code></pre>
  
  <p>So now, after eliminating "noise", my data.table contains only the values that correlate to my "signal". At this point it looks like this:    </p>
  
  <pre><code>    V1 V2 V3 V4 V5
   1: NA NA NA  2 NA
   2: NA NA  2 NA NA
   3: NA  2 NA NA  2
   4:  2 NA NA  2 NA
   5: NA NA  2 NA NA
   6: NA  2 NA NA  2
   7:  2 NA NA  2 NA
   8: NA NA  2 NA NA
   9: NA  2 NA NA NA
  10:  2 NA NA NA NA
  </code></pre>
  
  <p>I am working out how to export these values while recording their respective location in the thermal image. I need to export each observation and respective row and column information to another data.table. The purpose of this is to render another layer in the <code>rgl</code> scene and further analysis, like euclidean distance calculations. </p>
  
  <p>The hypothetical output is as follows:</p>
  
  <pre><code>y &lt;- c(1,2,3,4,5,1,2,3,4,5,1,2,3,4)
  x &lt;- c(10,9,8,7,6,7,6,5,4,3,4,3,2,1)
  z &lt;- c(2,2,2,2,2,2,2,2,2,2,2,2,2,2)
  
  aa &lt;- data.table(x,y,z)
  </code></pre>
  
  <p>And would look like this:</p>
  
  <pre><code>     x y z
   1: 10 1 2
   2:  9 2 2
   3:  8 3 2
   4:  7 4 2
   5:  6 5 2
   6:  7 1 2
   7:  6 2 2
   8:  5 3 2
   9:  4 4 2
  10:  3 5 2
  11:  4 1 2
  12:  3 2 2
  13:  2 3 2
  14:  1 4 2
  </code></pre>
  
  <p>Allowing me to create an <code>rgl</code> scene like one rendered with the following code:    </p>
  
  <pre><code>xb &lt;- c(1:nrow(bb)); yb &lt;- c(1:ncol(bb))
  
  persp3d(x= xb, y= yb, z= bb, col = "blue")+
  lines3d(aa$x[1:5], aa$y[1:5],  z= z+1, col = "green", lwd = 5)+  # my signal
  lines3d(aa$x[6:10], aa$y[6:10], z= z+1,col = "green", lwd = 5)+
  lines3d(aa$x[11:14], aa$y[11:14], z= z+1, col = "green", lwd = 5)
  </code></pre>
  
  <p><strong>Again, how can I export values from a data.table (or matrix) while simultaneously recording column and row number for each value?</strong></p>
  
  <p>Thanks for advice</p>