# What is the easiest way to create a vector with elements c1, c2, c3, ..., c1000

**URL:** <https://discourse.julialang.org/t/what-is-the-easiest-way-to-create-a-vector-with-elements-c1-c2-c3-c1000/93180>\
**Category:** New to Julia\
**Created:** [January 18, 2023, 8:54pm UTC](https://discourse.julialang.org/t/what-is-the-easiest-way-to-create-a-vector-with-elements-c1-c2-c3-c1000/93180 "2023-01-18T20:54:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_ricci](https://avatars.discourse-cdn.com/v4/letter/a/a4c791/32.png) [@Alex\_ricci](https://discourse.julialang.org/u/Alex_ricci)\
**Post date:** [January 18, 2023, 8:54pm UTC](https://discourse.julialang.org/t/what-is-the-easiest-way-to-create-a-vector-with-elements-c1-c2-c3-c1000/93180/1 "2023-01-18T20:54:33Z")

</div>

What is the easiest way to create a vector with elements c1, c2, c3, …, c1000

---

<div class="post-metadata">

**Author:** ![Eveee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eveee/32/45034_2.png) [@Eveee](https://discourse.julialang.org/u/Eveee)\
**Post date:** [January 18, 2023, 9:00pm UTC](https://discourse.julialang.org/t/what-is-the-easiest-way-to-create-a-vector-with-elements-c1-c2-c3-c1000/93180/2 "2023-01-18T21:00:38Z")

</div>

What do you mean with elements c1, … ? If they are concatenated letters and numbers you could do:

```julia
["c"*string(i) for i in 1:1000]

```

---

<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:** [January 18, 2023, 9:12pm UTC](https://discourse.julialang.org/t/what-is-the-easiest-way-to-create-a-vector-with-elements-c1-c2-c3-c1000/93180/3 "2023-01-18T21:12:47Z")

</div>

> [@Alex\_ricci](#):
>
> What is the easiest way to create a vector with elements c1, c2, c3, …, c1000

If you mean that you have variables with these names then could use metaprogramming, e.g. if they are global variable you could use `[eval(Symbol(:c, i)) for i = 1:1000]`.

However, in this case I would strongly urge you to re-think your code structure, and use a data structure like an array to begin with, not zillions of variables with different names.

---

<div class="post-metadata">

**Author:** ![de-souza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/de-souza/32/43417_2.png) [@de-souza](https://discourse.julialang.org/u/de-souza)\
**Post date:** [January 18, 2023, 9:53pm UTC](https://discourse.julialang.org/t/what-is-the-easiest-way-to-create-a-vector-with-elements-c1-c2-c3-c1000/93180/4 "2023-01-18T21:53:40Z")

</div>

See also: [Creating a list of symbols using a comprehension](https://discourse.julialang.org/t/88524)

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [January 18, 2023, 9:54pm UTC](https://discourse.julialang.org/t/what-is-the-easiest-way-to-create-a-vector-with-elements-c1-c2-c3-c1000/93180/5 "2023-01-18T21:54:17Z")

</div>

This generates a 1000-element vector of random values:

```julia
julia> c = rand(1000)
1000-element Vector{Float64}:
 0.5647389507376179
 0.18111369557568624
 0.8727956817721915
 0.22835918313738857
 0.2497456673875318
 0.9181345802141495
 0.6070152421017295
 0.7392933645606421
 0.6196225753003001
 0.280834358470918
 0.5100254226676332
 0.9816413192259156
 0.6700354181216701
 ⋮
 0.7927264973496545
 0.17482774804441292
 0.5294179310633541
 0.858583692569896
 0.1277444673489978
 0.9421766813356514
 0.48838842851956077
 0.3351709485105858
 0.4807149298155149
 0.3762423751393996
 0.45065876319192155
 0.09364657772281326

```

You can access c\_1, c\_2, c\_3, and c\_{1000} by using square brackets like so:

```julia
julia> c[1], c[2], c[3], c[1000]
(0.5647389507376179, 0.18111369557568624, 0.8727956817721915, 0.09364657772281326)

```

If there’s some relation between the vector index `i` and the element values of `c`, you can use an _array comprehension_ to populate `c` in accordance with that. For example, the elements of this vector follow the relation, c\_i = i^2.

```julia
julia> c = [i^2 for i = 1:1000]
1000-element Vector{Int64}:
       1
       4
       9
      16
      25
      36
      49
      64
      81
     100
     121
     144
     169
       ⋮
  978121
  980100
  982081
  984064
  986049
  988036
  990025
  992016
  994009
  996004
  998001
 1000000

```

---

<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:** [January 19, 2023, 12:53am UTC](https://discourse.julialang.org/t/what-is-the-easiest-way-to-create-a-vector-with-elements-c1-c2-c3-c1000/93180/6 "2023-01-19T00:53:54Z")

</div>

A post was split to a new topic: [Encouraging clearer questions on discourse?](https://discourse.julialang.org/t/encouraging-clearer-questions-on-discourse/93187)

---

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [January 19, 2023, 1:44am UTC](https://discourse.julialang.org/t/what-is-the-easiest-way-to-create-a-vector-with-elements-c1-c2-c3-c1000/93180/7 "2023-01-19T01:44:09Z")

</div>

I’d note that common practice is to initialize an array with e.g. `Vector{Type}(undef, 1000)`, rather than `rand(1000)`. `rand` will take more time to generate the random numbers and is conceptually less clear. `ones(1000)` also works.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [January 19, 2023, 8:14am UTC](https://discourse.julialang.org/t/what-is-the-easiest-way-to-create-a-vector-with-elements-c1-c2-c3-c1000/93180/8 "2023-01-19T08:14:59Z")

</div>

Nooo don’t introduce a newbie to `undef` allocation yet! 😅

It’s usually best to initiate/fill the array immediately upon allocating it, which is what `zeros`, `ones`, `fill`, `rand`, and comprehensions do.

Consider `undef` allocation for serious optimization work, but do it with caution because if you do it wrong, bad things can happen ⚠
