# Binding vectors to array

**URL:** <https://discourse.julialang.org/t/binding-vectors-to-array/10483>\
**Category:** General Usage\
**Created:** [April 22, 2018, 5:46pm UTC](https://discourse.julialang.org/t/binding-vectors-to-array/10483 "2018-04-22T17:46:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![economyboy](https://avatars.discourse-cdn.com/v4/letter/e/eb8c5e/32.png) [@economyboy](https://discourse.julialang.org/u/economyboy)\
**Post date:** [April 22, 2018, 5:46pm UTC](https://discourse.julialang.org/t/binding-vectors-to-array/10483/1 "2018-04-22T17:46:37Z")

</div>

Hey, I’m new to Julia and not sure about some issues regarding arrays. One of these is this one:

I just want to bind several vectors with zeros to an array. Their length is given in another array like that:

`length = [10 20 30 40....]`

Now I do the following:

```julia
zeroarray = zeros(length[1])
zeroarray = vcat([zeroarray], zeros(length[2])

for i in 3:length(length)
zeroarray = vcat(zeroarray, zeros(length[i])
end

```

This looks not like the appropriate approach to me. I would like to know how to formulate this properly. For explanation, this is the equivalent in python:

```julia
import numpy as np

length = [10, 20, 30, 40,....]
zeroarray = [np.zeros(length[0])]

for i in range(1, len(length)):
zeroarray.append(np.zeros(length[i]))

```

I would be very thankful for further advice!

---

<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:** [April 22, 2018, 5:49pm UTC](https://discourse.julialang.org/t/binding-vectors-to-array/10483/2 "2018-04-22T17:49:18Z")

</div>

Does

`zeros.(length)` work? I.e. using broadcasting.

You probably want to write length using commas instead of spaces, to give a vector instead of a matrix.

---

<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:** [April 22, 2018, 5:49pm UTC](https://discourse.julialang.org/t/binding-vectors-to-array/10483/3 "2018-04-22T17:49:56Z")

</div>

(By the way, it would be useful to see what kind of output you are looking for.)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 22, 2018, 6:25pm UTC](https://discourse.julialang.org/t/binding-vectors-to-array/10483/4 "2018-04-22T18:25:50Z")

</div>

**Edit:** I misunderstood your question. You can do this, though, which is similar:

```julia
zeroarray = vcat((zeros(n) for n in lengths)...)

```

Old answer:

First of all, you shouldn’t call your vector `length`, since that is the name of a very important function in julia (and in many other languages.)

The best approach seems to me to be a comprehension:

```julia
lengths = [10, 20, 30, 40] # or lengths = 10:10:40 
zeroarray = [zeros(n) for n in lengths]

```

In Python you would do exactly the same:

```julia
zeroarray = [np.zeros(n) for n in lengths]

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 22, 2018, 6:40pm UTC](https://discourse.julialang.org/t/binding-vectors-to-array/10483/5 "2018-04-22T18:40:55Z")

</div>

Actually, the best (and fastest) solution is probably

```julia
zeroarray = zeros(sum(lengths))

```

---

<div class="post-metadata">

**Author:** ![economyboy](https://avatars.discourse-cdn.com/v4/letter/e/eb8c5e/32.png) [@economyboy](https://discourse.julialang.org/u/economyboy)\
**Post date:** [April 22, 2018, 8:51pm UTC](https://discourse.julialang.org/t/binding-vectors-to-array/10483/6 "2018-04-22T20:51:04Z")

</div>

Oh yeah, that does work indeed! I completely forgot about the possibility of using zeros.() . And yes, I want to write it with commas 🙂.  
Thank you very much!

---

<div class="post-metadata">

**Author:** ![economyboy](https://avatars.discourse-cdn.com/v4/letter/e/eb8c5e/32.png) [@economyboy](https://discourse.julialang.org/u/economyboy)\
**Post date:** [April 22, 2018, 8:51pm UTC](https://discourse.julialang.org/t/binding-vectors-to-array/10483/7 "2018-04-22T20:51:05Z")

</div>

No, you got my question right 😃. Your original answer is what I was looking for. Thanks!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 23, 2018, 7:32am UTC](https://discourse.julialang.org/t/binding-vectors-to-array/10483/8 "2018-04-23T07:32:45Z")

</div>

> [@dpsanders](#):
>
> `zeros.(length)`

Oh, yeah. This is clearly the best solution, now that the original question has been clarified.
