[R] how to find how many modes in 2 dimensions case

Tony Plate tplate at acm.org
Sat Jun 9 16:21:44 CEST 2007


If you want to count the local maxima in the n x n matrix returned by 
kde2d, AND you know there are no ties, you could do something like the 
following:

 > set.seed(1)
 > x <- matrix(sample(10, 25, rep=TRUE), 5, 5)
 > x
      [,1] [,2] [,3] [,4] [,5]
[1,]    3    9    3    5   10
[2,]    4   10    2    8    3
[3,]    6    7    7   10    7
[4,]   10    7    4    4    2
[5,]    3    1    8    8    3
 > sum(x > cbind(0, x[,-5]) & x > cbind(x[,-1], 0) & x > rbind(x[-1,], 
0) & x > rbind(0, x[-5,]))
[1] 4
 >

Just be careful that your counting formula matches your definition of 
"neighbor" (the above formula does not include diagonal neighbors).

And of course, ties make things more complicated (note that the above 
simple algorithm misses the local maximum consisting of two 8's in the 
last row.)

-- Tony Plate


Patrick Wang wrote:
> Hi,
> 
> Does anyone know how to count the number of modes in 2 dimensions using
> kde2d function?
> 
> Thanks
> Pat
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list