Is there a central difference/gradient function somewhere?

something like this?

function centraldiff(v::AbstractMatrix)
    dv  = diff(v)/2
    a   = [dv[[1],:];dv]
    a .+= [dv;dv[[end],:]]
    a
end

function centraldiff(v::AbstractVector)
    dv  = diff(v)/2
    a   = [dv[1];dv]
    a .+= [dv;dv[end]]
    a
end

These methods are of course not optimized for performance, writing the loop manually would likely improve performance and reduce memory allocations.

Note, these functions do not reduce the length of the input array (as diff does), by copying the first and last diff elements.

1 Like