# Zeroing arrays

**URL:** <https://discourse.julialang.org/t/zeroing-arrays/6789>\
**Category:** New to Julia\
**Tags:** array\
**Created:** [October 31, 2017, 12:26am UTC](https://discourse.julialang.org/t/zeroing-arrays/6789 "2017-10-31T00:26:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [October 31, 2017, 12:26am UTC](https://discourse.julialang.org/t/zeroing-arrays/6789/1 "2017-10-31T00:26:32Z")

</div>

Is there a faster/more elegant way to constructing the following:

```julia
X = [zeros(2) for i=1:10]

```

to get an array of 2D vectors? Also, how important for performance is it to preallocate such arrays, as opposed to concatenating them?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 31, 2017, 12:38am UTC](https://discourse.julialang.org/t/zeroing-arrays/6789/2 "2017-10-31T00:38:49Z")

</div>

That does not create an array of “2D” vectors (it creates an array/vector of vectors/1D arrays) and it does not concatenate anything. If you actually want exactly what `X` is than it’s about the best way to do it. If you want something else, you should specify exactly what you want, e.g. 2D array with `zeros(2, 10)`. Finally how important for performance something is not a question possible to answer, or the answer is always, depend on how important it is to you.

---

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [October 31, 2017, 12:45am UTC](https://discourse.julialang.org/t/zeroing-arrays/6789/3 "2017-10-31T00:45:30Z")

</div>

Got it. Yes, I want what the above code actually produces. I’ll stick with that, then.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [October 31, 2017, 12:58am UTC](https://discourse.julialang.org/t/zeroing-arrays/6789/4 "2017-10-31T00:58:42Z")

</div>

What about `[@SVector zeros(2) for i in 1:10]`? then the internal arrays can be stack allocated and do other things well. If you know they are all going to be small, this can be fast.

---

<div class="post-metadata">

**Author:** ![garrison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/garrison/32/209519_2.png) [@garrison](https://discourse.julialang.org/u/garrison)\
**Post date:** [October 31, 2017, 1:29am UTC](https://discourse.julialang.org/t/zeroing-arrays/6789/5 "2017-10-31T01:29:42Z")

</div>

> [@ChrisRackauckas](#):
>
> What about [@SVector zeros(2) for i in 1:10]?

Note also that `@SVector` is from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl).

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [October 31, 2017, 2:46am UTC](https://discourse.julialang.org/t/zeroing-arrays/6789/6 "2017-10-31T02:46:36Z")

</div>

Or SVector(0.0, 0.0) which is both shorter and more explicit?
