[R] circular filter

Peter Wolf s-plus at wiwi.uni-bielefeld.de
Wed Feb 25 08:46:16 CET 2004


try:

filter.matrix.center<-function(n=9,size=5){
  x<-matrix(1,n,n)
  center<-(n+1)/2
  (abs(row(x)-center)+abs(col(x)-center)) < size
}
filter.matrix.center()

some tests:

 > 0+filter.matrix.center(5,2)
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    0    0
[2,]    0    0    1    0    0
[3,]    0    1    1    1    0
[4,]    0    0    1    0    0
[5,]    0    0    0    0    0

 > 0+filter.matrix.center()
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
 [1,]    0    0    0    0    1    0    0    0    0
 [2,]    0    0    0    1    1    1    0    0    0
 [3,]    0    0    1    1    1    1    1    0    0
 [4,]    0    1    1    1    1    1    1    1    0
 [5,]    1    1    1    1    1    1    1    1    1
 [6,]    0    1    1    1    1    1    1    1    0
 [7,]    0    0    1    1    1    1    1    0    0
 [8,]    0    0    0    1    1    1    0    0    0
 [9,]    0    0    0    0    1    0    0    0    0

Peter Wolf

--------------------------------------------------------------------------------------------------------

Christof Bigler wrote:
I try to find a circular filter that I can export to be used in a 
spatial software.
Assuming, we have a matrix, representing 9x9 regularly spaced points  
with the center point 'filter[5, 5]'. In this example, I want to find a 
function that weighs all neighbor points within a distance of d=4 units 
with 1:

 > filter <- matrix(0, 9, 9)
 > filter <- function() ...
 > filter
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
 [1,]    0    0    0    0    1    0    0    0    0
 [2,]    0    0    1    1    1    1    1    0    0
 [3,]    0    1    1    1    1    1    1    1    0
 [4,]    0    1    1    1    1    1    1    1    0
 [5,]    1    1    1    1    1    1    1    1    1
 [6,]    0    1    1    1    1    1    1    1    0
 [7,]    0    1    1    1    1    1    1    1    0
 [8,]    0    0    1    1    1    1    1    0    0
 [9,]    0    0    0    0    1    0    0    0    0

Finally, I want to use a larger matrix, e.g. with 61x61 points. Is there 
a simple function around that I could use to this end?

Thanks!
Christof

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! 
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list