1

What I am looking for is something like convolution but with a function that takes the values in a neighbourhood as parameters. exemple:

m <- matrix(1:25, nrow = 5)
f <- function(x) {sd(x, na.rm=T)/mean(x, na.rm=T)}    
m

      [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16   21
[2,]    2    7   12   17   22
[3,]    3    8   13   18   23
[4,]    4    9   14   19   24
[5,]    5   10   15   20   25

with a 3x3 kernel the result should be:

m.out[1,1] = f(c(NA, NA, NA, NA, NA,  1, 2, 6, 7)) = 0.7359801
m.out[2,1] = f(c(NA, NA, NA,  1, 2, 3, 6, 7, 8)) = 0.640216
m.out[2,2] = f(c(1, 2, 3, 6, 7, 8, 11, 12, 13)) =  0.6308401

m.out[5,5] = f(c(19, 20, 24, 25, NA, NA, NA, NA, NA)) = 0.1338146

and so...

In fact this is close to the filters in image packages but I did not succeed to put my own function... thanks a lot Leon

  • 1
    Perhaps [this](http://stackoverflow.com/questions/29105175/find-neighbouring-elements-of-a-matrix-in-r) helps – akrun Apr 02 '17 at 08:54
  • @akrun thank you, very useful, I am reassured the solution with expand.grid and two loops this is what I found best, I hoped there is a solution in an image package... I have 2024x2024 images, so I guess this is maybe not the good approach – Leon Espinosa Apr 02 '17 at 09:25

0 Answers0