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