# How to generate independent vectors via for-loop?

**URL:** <https://discourse.julialang.org/t/how-to-generate-independent-vectors-via-for-loop/93584>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 26, 2023, 4:14pm UTC](https://discourse.julialang.org/t/how-to-generate-independent-vectors-via-for-loop/93584 "2023-01-26T16:14:14Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [January 26, 2023, 4:14pm UTC](https://discourse.julialang.org/t/how-to-generate-independent-vectors-via-for-loop/93584/1 "2023-01-26T16:14:14Z")

</div>

At a point in my real code, I want to generate a number of independent vectors based on parameter `N`(which is calculated at a certain point in the code). For example, if `N=3`, then I need to generate the following vectors:

```julia
vector_1 = [0,0];
vector_2 = [0,0,0];
vector_3 = [0,0,0,0];

```

So, I tried to write something like below. However, there are two problems:  
1- Syntax of `vector_"$i"` gives error . How to write the correct syntax?  
2- Assuming the syntax is correct, an error is given at `S = vector_"$i"` since the vectors are not seen out of the `for-loop` scope. How to make the generated vectors seen out of `for-loop` scope?

```julia
function dd()
N =3;
for i = 1:N
    vector_"$i" = zeros(i+1); # gives `ERROR: UndefVarError: @vector__str not defined`
end
S = vector_1; # gives `ERROR: UndefVarError: vector_1 not defined`
end
dd()

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [January 26, 2023, 4:28pm UTC](https://discourse.julialang.org/t/how-to-generate-independent-vectors-via-for-loop/93584/2 "2023-01-26T16:28:07Z")

</div>

Store these vectors in a dictionary. Don’t try to make them variables.

```julia
function dd()
    N = 3
    out Dict()
    for i = 1:N
        out[i] = zeros(i + 1)
    end
    return out
end

vector_dict = dd()

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 26, 2023, 4:33pm UTC](https://discourse.julialang.org/t/how-to-generate-independent-vectors-via-for-loop/93584/3 "2023-01-26T16:33:53Z")

</div>

or a vector of vectors

```julia
julia> vectors = Vector{Int}[]
       for i in 1:3
           push!(vectors, zeros(i))
       end

julia> vectors
3-element Vector{Vector{Int64}}:
 [0]
 [0, 0]
 [0, 0, 0]

```

(also I would recommend typing the `Dict`, if the case `Dict{Int,Vector{Int}}(()`:

```julia
julia> d = Dict{Int,Vector{Int}}()
Dict{Int64, Vector{Int64}}()

julia> for i in 1:3
           d[i] = zeros(i)
       end

julia> d
Dict{Int64, Vector{Int64}} with 3 entries:
  2 => [0, 0]
  3 => [0, 0, 0]
  1 => [0]

```

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [January 26, 2023, 4:39pm UTC](https://discourse.julialang.org/t/how-to-generate-independent-vectors-via-for-loop/93584/4 "2023-01-26T16:39:27Z")

</div>

Or even as a tuple

```julia
julia> ntuple(i->zeros(i+1), 3)
([0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0])

```

You _can_ dynamically generate variables using `eval`, but they will necessarily live in global scope. Don’t do it. Seriously. It leads to hard to read and reason about code, and a cluttered global namespace. Nevertheless:

```julia
julia> for i = 1:N
           var = Symbol("vector_$i")
           eval( :( $var = zeros($i+1) ));
       end

julia> vector_1
2-element Vector{Float64}:
 0.0
 0.0

julia> vector_2
3-element Vector{Float64}:
 0.0
 0.0
 0.0

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [January 26, 2023, 4:40pm UTC](https://discourse.julialang.org/t/how-to-generate-independent-vectors-via-for-loop/93584/5 "2023-01-26T16:40:20Z")

</div>

> [@lmiq](#):
>
> ```julia
> vectors = Vector{Int}[]
> for i in 1:3
> push!(vectors, zeros(i))
> end
> 
> ```

Thanks for your comment. The issue here is that I need to connect the generated vectors to other ones (which are changing in time-domain loop. For example, `vectors[2] = @view(D[:])`. However, a change in `D` will not be updated in `vectors[2]`/

```julia
D = [55,69];
vectors = Vector{Int}[]
       for i in 1:3
           push!(vectors, zeros(i))
       end

vectors[2] = @view(D[:]);
D[1] = 77;
julia> vectors
3-element Vector{Vector{Int64}}:
 [0]
 [55, 69]
 [0, 0, 0]

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [January 26, 2023, 4:45pm UTC](https://discourse.julialang.org/t/how-to-generate-independent-vectors-via-for-loop/93584/6 "2023-01-26T16:45:26Z")

</div>

Thank you very much for your note. Can I restrict the generated vectors to be ONLY in scope of dd() function?

```julia
function dd()
N=3
for i = 1:N
    var = Symbol("vector_$i")
    eval( :( $var = zeros($i+1) ));
end
end

dd()

```

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [January 26, 2023, 4:47pm UTC](https://discourse.julialang.org/t/how-to-generate-independent-vectors-via-for-loop/93584/7 "2023-01-26T16:47:12Z")

</div>

> [@Amro](#):
>
> For example, `vectors[2] = @view(D[:])`. However, a change in `D` will not be updated in `vectors[2]`

That’s because `vectors` is of type `Vector{Vector{Int}}` whereas `@view(D[:])` is a subarray. Upon assignment `vector[2] = @view(D[:])` a conversion to the former type, i.e. an implicit copy is made.  
You could widen the container type

```julia
julia> vectors = AbstractVector{Int}[]

julia> for i in 1:3
           push!(vectors, zeros(i))
       end

julia> vectors
3-element Vector{AbstractVector{Int64}}:
 [0]
 [0, 0]
 [0, 0, 0]

julia> vectors[2] = @view(D[:]);

julia> vectors
3-element Vector{AbstractVector{Int64}}:
 [0]
 [55, 69]
 [0, 0, 0]

julia> vectors[2]
2-element view(::Vector{Int64}, :) with eltype Int64:
 55
 69

julia> D[1] = 77
77

julia> vectors
3-element Vector{AbstractVector{Int64}}:
 [0]
 [77, 69]
 [0, 0, 0]

```

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [January 26, 2023, 4:50pm UTC](https://discourse.julialang.org/t/how-to-generate-independent-vectors-via-for-loop/93584/8 "2023-01-26T16:50:25Z")

</div>

> [@Amro](#):
>
> Can I restrict the generated vectors to be ONLY in scope of dd() function?

No, that’s what I stated. `eval` evaluates an expression in the global scope of the module.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 26, 2023, 4:50pm UTC](https://discourse.julialang.org/t/how-to-generate-independent-vectors-via-for-loop/93584/9 "2023-01-26T16:50:51Z")

</div>

> [@skleinbo](#):
>
> Upon assignment `vector[2] = @view(D[:])` a conversion to the former type

IMHO this should just error.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [January 26, 2023, 4:54pm UTC](https://discourse.julialang.org/t/how-to-generate-independent-vectors-via-for-loop/93584/10 "2023-01-26T16:54:48Z")

</div>

> [@skleinbo](#):
>
> ```julia
> julia> vectors = AbstractVector{Int}[]
> 
> julia> for i in 1:3
> push!(vectors, zeros(i))
> end
> 
> ```

Thank you very much!  
From a performance point of view, Do the values of vectors[2] are allocated in order in memory (column-major)? In other words, do working on the vector `vector_2` is the same as on `vectors[2]`?

```julia
vector_1 = [0,0];
vector_2 = [0,0,0];
vector_3 = [0,0,0,0];

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 26, 2023, 4:57pm UTC](https://discourse.julialang.org/t/how-to-generate-independent-vectors-via-for-loop/93584/11 "2023-01-26T16:57:14Z")

</div>

Performance-wise this will be quite bad in general, because you are handling a container with an abstract type.

If you need performance there you probably want something else, and this becomes a [xy problem](https://xyproblem.info/).

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [January 26, 2023, 5:44pm UTC](https://discourse.julialang.org/t/how-to-generate-independent-vectors-via-for-loop/93584/12 "2023-01-26T17:44:45Z")

</div>

> [@Amro](#):
>
> Do the values of vectors[2] are allocated in order in memory (column-major)?

Yes, they form a concrete vector. Accessing `vectors[2]` is bad performance-wise because of the abstract container type. If you can live with that depends on the kind of work and how much indexing is done. Using a function barrier might be sufficient to make the overhead from the type instability due to indexing into `vectors` negligible.

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [January 26, 2023, 5:51pm UTC](https://discourse.julialang.org/t/how-to-generate-independent-vectors-via-for-loop/93584/13 "2023-01-26T17:51:44Z")

</div>

Another option if you know how many you want at compile time:

```julia
sizes = (2,3,4)
Base.Cartesian.@nexprs 3 j->vector_j = zeros(sizes[j])

```

Now the variables `vector_1`, `vector_2`, and `vector_3` exist with the sizes you have asked for.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [January 26, 2023, 7:07pm UTC](https://discourse.julialang.org/t/how-to-generate-independent-vectors-via-for-loop/93584/14 "2023-01-26T19:07:07Z")

</div>

> [@lmiq](#):
>
> If you need performance there you probably want something else, and this becomes a [xy problem](https://xyproblem.info/)

I am sorry if I could not deliver my point clearly from the beginning.  
Thanks for pointing this out. I will be more precise and accurate next times to make the discussion more productive.  
From a performance point of view, can you recommend someting?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 26, 2023, 7:15pm UTC](https://discourse.julialang.org/t/how-to-generate-independent-vectors-via-for-loop/93584/15 "2023-01-26T19:15:51Z")

</div>

> [@Amro](#):
>
> From a performance point of view, can you recommend someting?

That depends a lot on the performance of what. It is not possible to answer that without further information.
