[R] circular filter

Peter Wolf s-plus at wiwi.uni-bielefeld.de
Wed Feb 25 09:22:24 CET 2004


filter.matrix.center  implements Manhattan or L 1 distance.
If you want to define neighbor points
by Euklidean distances (L 2) use filter.matrix.center(p=2):

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

example:

 > 0+filter.matrix.center.p(9,4.1)
    [,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

Peter Wolf



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

Peter Wolf wrote:

>
> 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
>
> ______________________________________________
> 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