# Array with constant values

**URL:** <https://discourse.julialang.org/t/array-with-constant-values/51866>\
**Category:** New to Julia\
**Created:** [December 15, 2020, 12:22pm UTC](https://discourse.julialang.org/t/array-with-constant-values/51866 "2020-12-15T12:22:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ishihama](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@ishihama](https://discourse.julialang.org/u/ishihama)\
**Post date:** [December 15, 2020, 12:22pm UTC](https://discourse.julialang.org/t/array-with-constant-values/51866/1 "2020-12-15T12:22:26Z")

</div>

I wish to create an array whose length is 102 and having constant values everywhere without defining a `for-loop` explicitly. Assuming such an array `ArrayWithConst`, if I define it like `ArrayWithConst[1:end]=5.0`, this generates an error. I wonder how to implement this. Thanks in advance for kind advices.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [December 15, 2020, 12:24pm UTC](https://discourse.julialang.org/t/array-with-constant-values/51866/2 "2020-12-15T12:24:38Z")

</div>

```julia
fill(5.0, 102)

```

Is this what you are looking for?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [December 15, 2020, 1:25pm UTC](https://discourse.julialang.org/t/array-with-constant-values/51866/3 "2020-12-15T13:25:07Z")

</div>

Also, your code is almost right. If you already have `ArrayWithConst`, then setting all of its values to `5.0` can be done by `ArrayWithConst[1:end] .= 5.0` (note the dot before the `=`).

---

<div class="post-metadata">

**Author:** ![ishihama](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@ishihama](https://discourse.julialang.org/u/ishihama)\
**Post date:** [December 15, 2020, 9:53pm UTC](https://discourse.julialang.org/t/array-with-constant-values/51866/4 "2020-12-15T21:53:07Z")

</div>

thanks!

---

<div class="post-metadata">

**Author:** ![ishihama](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@ishihama](https://discourse.julialang.org/u/ishihama)\
**Post date:** [December 15, 2020, 9:55pm UTC](https://discourse.julialang.org/t/array-with-constant-values/51866/5 "2020-12-15T21:55:08Z")

</div>

thanks. this does not work for me (type mismatch error). I first defined `xd=1.5:0.05:6.55`, then `ArrayWithConstant=copy(xd)` for preparation.

---

<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:** [December 15, 2020, 10:04pm UTC](https://discourse.julialang.org/t/array-with-constant-values/51866/6 "2020-12-15T22:04:34Z")

</div>

> [@ishihama](#):
>
> this does not work for me (type mismatch error). I first defined `xd=1.5:0.05:6.55` , then `ArrayWithConstant=copy(xd)` for preparation.

You need a mutable copy, not a range (which is returned by `copy` of a range). Try `Vector(xd)` instead of `copy(xd)`, for example (or `Base.copymutable(xd)`).

(But better yet, just do `fill(5.0, length(xd))` — it is pointless to make a copy just to overwrite it.)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 15, 2020, 10:10pm UTC](https://discourse.julialang.org/t/array-with-constant-values/51866/7 "2020-12-15T22:10:43Z")

</div>

Why not the basic: `ArrayWithConst = zeros(102) .+ 5.0` ?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 15, 2020, 10:28pm UTC](https://discourse.julialang.org/t/array-with-constant-values/51866/8 "2020-12-15T22:28:44Z")

</div>

That’s almost certainly 2x slower (with not much gain).
