As part of what will hopefully become a larger post, I’m interested in finding an R way to achieve the following: given an n x n matrix of zeroes with a single non-zero element of some value v, fill the surrounding entries such that each other element is at most one less than those surrounding it (up or down). For example, with an 8x8 matrix with a value of 5 at c(5, 5), the result would be

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    0    0    0    0    1    0    0    0
[2,]    0    0    0    1    2    1    0    0
[3,]    0    0    1    2    3    2    1    0
[4,]    0    0    2    3    4    3    2    1
[5,]    1    2    3    4    5    4    3    2
[6,]    0    1    2    3    4    3    2    1
[7,]    0    0    1    2    3    2    1    0
[8,]    0    0    0    1    2    1    0    0

This is somewhat akin to imposing a contour density on top of a single peak, but I really can’t find any suitable approaches. Convolutions came to mind, but I can’t think of or find the appropriate kernel.

Let me know if you have one!

Update:

Thanks to June Choe, this code using outer() produces the desired matrix for a point at c(vx, vy) with value vv in a n x n matrix

vx <- 4
vy <- 3
vv <- 5
n <- 8
outer(1:n, 1:n, function(x, y) pmax(vv - abs(x - vx) - abs(y - vy), 0))
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    0    1    2    1    0    0    0    0
[2,]    1    2    3    2    1    0    0    0
[3,]    2    3    4    3    2    1    0    0
[4,]    3    4    5    4    3    2    1    0
[5,]    2    3    4    3    2    1    0    0
[6,]    1    2    3    2    1    0    0    0
[7,]    0    1    2    1    0    0    0    0
[8,]    0    0    1    0    0    0    0    0