# Best way to shift a window around a multidimensional Array?

**URL:** https://discourse.julialang.org/t/best-way-to-shift-a-window-around-a-multidimensional-array/107219
**Category:** General Usage
**Created:** [December 6, 2023, 2:39pm UTC](https://discourse.julialang.org/t/best-way-to-shift-a-window-around-a-multidimensional-array/107219 "2023-12-06T14:39:58Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![capri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/capri/32/39015_2.png) [@capri](https://discourse.julialang.org/u/capri)
#### Post date: [December 6, 2023, 2:39pm UTC](https://discourse.julialang.org/t/best-way-to-shift-a-window-around-a-multidimensional-array/107219/1 "2023-12-06T14:39:58Z")

</div>

Okay so let’s say I have a multidimensional Array `m` and I want to extract a window around a center point that has a size of 3 (for example) in every dimension.

The following code does what I want:

```julia
m = rand(5,5,5)

center = [2,3,4]

window = m[[center[i]-1:center[i]+1 for i in 1:ndims(m)]...]

```

So far so good.  
Now, I want to do this for different center points - at every time step of a huge simulation - and for this application my code looks a bit unefficient to me. It seems to me that there should be a way to create the window once and then shift it around different centerpoints - maybe with a `@views`? I just have not found a way to do it yet.

Does anyone have an idea how to do this more efficiently?

edit: I just discovered that my code does not work when choosing a center close to an edge. I forgot to mention that it should wrap around on the edges.

---

<div class="post-metadata">

### Author: ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)
#### Post date: [December 6, 2023, 2:48pm UTC](https://discourse.julialang.org/t/best-way-to-shift-a-window-around-a-multidimensional-array/107219/2 "2023-12-06T14:48:28Z")

</div>

It depends what you want to do, but I found the following explanations quite useful when writing multidimensional codes

> **[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: [December 6, 2023, 2:53pm UTC](https://discourse.julialang.org/t/best-way-to-shift-a-window-around-a-multidimensional-array/107219/3 "2023-12-06T14:53:55Z")

</div>

> [@capri](#):
>
> Does anyone have an idea how to do this more efficiently?

This is essentially the same as the problem discussed here: [Matrix Free Differentiation - #3 by stevengj](https://discourse.julialang.org/t/matrix-free-differentiation/107134/3)
