Skip to content

Finding local maxima on a loess surface

2 messages · Diego Rojas, David Winsemius

#
On May 10, 2012, at 12:10 AM, Diego Rojas wrote:

            
hasmax <- function(mtx, x, y)  if(  (mtx[x,y] > mtx[x,y-1]) &
  (mtx[x,y] > mtx[x,y+1]) &
  (mtx[x,y] > mtx[x-1,y]) &
  (mtx[x,y] > mtx[x+1,y]) ) {return(TRUE ) } else {return(FALSE)}

for(x in 3:(dim(topo.pred)[1] -4)) {
  for(y in 3:(dim(topo.pred)[2]-4) )  {
  if( hasmax(topo.pred, x , y) ){print(c(x,y))}  }}
#
[1] 40  7

Note: that topo.pred has a border of two and three row/columns of NA's  
that made this very annoying to debug. A proper function would  
probably need to pre-qualify the index ranges.


I tried a sign change approach but generalizing to 2d created  
conceptual difficulties I could not resolve, so I just checked in both  
directions for the local point being greater than its neighbors.  You  
obviously could do something other than printing coordinates at a  
maximum