# Optimal way of checking neighboring elements in matrix

**URL:** <https://discourse.julialang.org/t/optimal-way-of-checking-neighboring-elements-in-matrix/30404>\
**Category:** New to Julia\
**Tags:** indexing\
**Created:** [October 28, 2019, 2:40pm UTC](https://discourse.julialang.org/t/optimal-way-of-checking-neighboring-elements-in-matrix/30404 "2019-10-28T14:40:06Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 28, 2019, 3:31pm UTC](https://discourse.julialang.org/t/optimal-way-of-checking-neighboring-elements-in-matrix/30404/3 "2019-10-28T15:31:52Z")

</div>

> [@Jonas\_Hjulstad](#):
>
> Is there a more efficient way than using if-statements for start/end columns and rows?

In many applications, I would add ghost elements at the edges of the array (extra rows and columns of zeros at the edges), and then just loop over the interior. See also [Arrays with periodic boundaries - #4 by stevengj](https://discourse.julialang.org/t/arrays-with-periodic-boundaries/4015/4)

Another advantage of ghost elements is that they are [seasonal](https://www.youtube.com/watch?v=kGiYxCUAhks).

---

_[View the full topic](https://discourse.julialang.org/t/optimal-way-of-checking-neighboring-elements-in-matrix/30404)._
