# What is the best way to calculate the numerical gradient of a matrix, representing multiple points in 3D space?

**URL:** <https://discourse.julialang.org/t/what-is-the-best-way-to-calculate-the-numerical-gradient-of-a-matrix-representing-multiple-points-in-3d-space/96727>\
**Category:** Numerics\
**Tags:** question\
**Created:** [March 28, 2023, 8:47pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-calculate-the-numerical-gradient-of-a-matrix-representing-multiple-points-in-3d-space/96727 "2023-03-28T20:47:15Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![Dale\_James\_Black](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dale_james_black/32/11595_2.png) [@Dale\_James\_Black](https://discourse.julialang.org/u/Dale_James_Black)\
**Post date:** [March 28, 2023, 9:51pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-calculate-the-numerical-gradient-of-a-matrix-representing-multiple-points-in-3d-space/96727/10 "2023-03-28T21:51:43Z")

</div>

If that’s the case then this is probably the simplest answer (modified from [this](https://discourse.julialang.org/t/is-there-a-central-difference-gradient-function-somewhere/19454/3))

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

```

---

_[View the full topic](https://discourse.julialang.org/t/what-is-the-best-way-to-calculate-the-numerical-gradient-of-a-matrix-representing-multiple-points-in-3d-space/96727)._
