# 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:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonas\_Hjulstad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonas_hjulstad/32/9146_2.png) [@Jonas\_Hjulstad](https://discourse.julialang.org/u/Jonas_Hjulstad)\
**Post date:** [October 28, 2019, 2:40pm UTC](https://discourse.julialang.org/t/optimal-way-of-checking-neighboring-elements-in-matrix/30404/1 "2019-10-28T14:40:06Z")

</div>

With any two-dimensional matrix, is there an optimal way of checking wether a neighboring element exists (and if it can be accessed)?

Example:

```julia
mat = [1 0 0 0 1
            0 1 1 1 0
            0 1 0 0 0
            0 1 0 0 0
            1 0 0 0 0]

```

for all mat[i,j] I want to check elements to the right/left/top/bottom, but only if that index exists. Is there a more efficient way than using if-statements for start/end columns and rows?

---

<div class="post-metadata">

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

</div>

If you’re doing this over all elements in the array, one of the the best ways is by smartly constructing the iteration space. Check out this blog post on writing multidimensional windowed loops:

> **[Multidimensional algorithms and iteration](https://julialang.org/blog/2016/02/iteration/)**
>
> Multidimensional algorithms and iteration | Julia makes it easy to write elegant and...

---

<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).

---

<div class="post-metadata">

**Author:** ![Jonas\_Hjulstad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonas_hjulstad/32/9146_2.png) [@Jonas\_Hjulstad](https://discourse.julialang.org/u/Jonas_Hjulstad)\
**Post date:** [October 28, 2019, 4:03pm UTC](https://discourse.julialang.org/t/optimal-way-of-checking-neighboring-elements-in-matrix/30404/4 "2019-10-28T16:03:31Z")

</div>

I did suspect guard-elements to be a good solution. I will test this, thanks!
