# How do i make a square lattice of (2n+1) x (2n+1)?

**URL:** <https://discourse.julialang.org/t/how-do-i-make-a-square-lattice-of-2n-1-x-2n-1/58158>\
**Category:** General Usage\
**Created:** [March 29, 2021, 9:53am UTC](https://discourse.julialang.org/t/how-do-i-make-a-square-lattice-of-2n-1-x-2n-1/58158 "2021-03-29T09:53:30Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![CashMoneyyyy](https://avatars.discourse-cdn.com/v4/letter/c/5f8ce5/32.png) [@CashMoneyyyy](https://discourse.julialang.org/u/CashMoneyyyy)\
**Post date:** [March 29, 2021, 9:53am UTC](https://discourse.julialang.org/t/how-do-i-make-a-square-lattice-of-2n-1-x-2n-1/58158/1 "2021-03-29T09:53:30Z")

</div>

I need to define a square lattice of size (2n+1) x (2n+1) and assign each cell as zero. Could anyone help me out on how to do this?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 29, 2021, 10:10am UTC](https://discourse.julialang.org/t/how-do-i-make-a-square-lattice-of-2n-1-x-2n-1/58158/2 "2021-03-29T10:10:09Z")

</div>

What have you tried so far?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [March 29, 2021, 10:14am UTC](https://discourse.julialang.org/t/how-do-i-make-a-square-lattice-of-2n-1-x-2n-1/58158/3 "2021-03-29T10:14:25Z")

</div>

Define a “lattice”.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [March 29, 2021, 10:47am UTC](https://discourse.julialang.org/t/how-do-i-make-a-square-lattice-of-2n-1-x-2n-1/58158/4 "2021-03-29T10:47:55Z")

</div>

Perhaps:

```julia
julia> n=2
2

julia> zeros(Float64,2n+1,2n+1)
5×5 Matrix{Float64}:
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0

```

if lattice=matrix 🙂

---

<div class="post-metadata">

**Author:** ![CashMoneyyyy](https://avatars.discourse-cdn.com/v4/letter/c/5f8ce5/32.png) [@CashMoneyyyy](https://discourse.julialang.org/u/CashMoneyyyy)\
**Post date:** [March 30, 2021, 2:13pm UTC](https://discourse.julialang.org/t/how-do-i-make-a-square-lattice-of-2n-1-x-2n-1/58158/5 "2021-03-30T14:13:30Z")

</div>

Hi, I haven’t got anywhere with it atm. I am really stuck with it.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 30, 2021, 2:15pm UTC](https://discourse.julialang.org/t/how-do-i-make-a-square-lattice-of-2n-1-x-2n-1/58158/6 "2021-03-30T14:15:59Z")

</div>

What is the context of your problem? Where does it come from and what are you going to do with the “lattice”?

---

<div class="post-metadata">

**Author:** ![CashMoneyyyy](https://avatars.discourse-cdn.com/v4/letter/c/5f8ce5/32.png) [@CashMoneyyyy](https://discourse.julialang.org/u/CashMoneyyyy)\
**Post date:** [March 30, 2021, 2:29pm UTC](https://discourse.julialang.org/t/how-do-i-make-a-square-lattice-of-2n-1-x-2n-1/58158/7 "2021-03-30T14:29:20Z")

</div>

This is what I was given: ![image](https://global.discourse-cdn.com/julialang/original/3X/f/b/fb71da7462df57b9199e3995502ad847002796e8.jpeg)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 30, 2021, 3:45pm UTC](https://discourse.julialang.org/t/how-do-i-make-a-square-lattice-of-2n-1-x-2n-1/58158/9 "2021-03-30T15:45:16Z")

</div>

Is this a homework question or an exam or something? We wouldn’t want to take away the learning experience for you (but we do want to help).

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [March 30, 2021, 3:47pm UTC](https://discourse.julialang.org/t/how-do-i-make-a-square-lattice-of-2n-1-x-2n-1/58158/10 "2021-03-30T15:47:15Z")

</div>

> [@mike\_k](#):
>
> I would use the (distance-)matrix approach suggested by @oheil

I would use the contents of the lessons/classes, which I perhaps have missed, by accident. My class mates may have some scripts or there has been some book suggested, which, after reading, would lead me more or less straight forward to the solution of this offering. But of course, that’s just me 😉

---

<div class="post-metadata">

**Author:** ![CashMoneyyyy](https://avatars.discourse-cdn.com/v4/letter/c/5f8ce5/32.png) [@CashMoneyyyy](https://discourse.julialang.org/u/CashMoneyyyy)\
**Post date:** [March 31, 2021, 11:42am UTC](https://discourse.julialang.org/t/how-do-i-make-a-square-lattice-of-2n-1-x-2n-1/58158/11 "2021-03-31T11:42:54Z")

</div>

It is a homework task

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [March 31, 2021, 11:49am UTC](https://discourse.julialang.org/t/how-do-i-make-a-square-lattice-of-2n-1-x-2n-1/58158/12 "2021-03-31T11:49:01Z")

</div>

Go for it on your own. Whenever you are stuck or strange errors come up you don’t understand, check back with us and we will help, if we just see that you are trying.

---

<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:** [March 31, 2021, 1:19pm UTC](https://discourse.julialang.org/t/how-do-i-make-a-square-lattice-of-2n-1-x-2n-1/58158/13 "2021-03-31T13:19:52Z")

</div>

Hint: It seems by “lattice” the problem means “2d array of values” representing a spatial grid.
