# How to create an array with a specified number and specified length

**URL:** <https://discourse.julialang.org/t/how-to-create-an-array-with-a-specified-number-and-specified-length/19779>\
**Category:** New to Julia\
**Created:** [January 18, 2019, 11:01am UTC](https://discourse.julialang.org/t/how-to-create-an-array-with-a-specified-number-and-specified-length/19779 "2019-01-18T11:01:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [January 18, 2019, 11:01am UTC](https://discourse.julialang.org/t/how-to-create-an-array-with-a-specified-number-and-specified-length/19779/1 "2019-01-18T11:01:35Z")

</div>

I would like to create an array with the value of 430, which repeats 5 times, so the output would look approximately like (this is simulated output):

```julia
julia> CC
5-element Array{Float64,1}:
 430.0
 430.0
 430.0
 430.0
 430.0

```

I have tried using the example from [here](https://en.wikibooks.org/wiki/Introducing_Julia/Arrays_and_tuples#Uninitialized) which looks like this

```julia
array = Array{Int64}(undef, 5)

```

and then specifying

```julia
CC = Array{Float64}(430.0, 5)

```

But this of course generates an error

```julia
ERROR: MethodError: no method matching Array{Float64,N} where N(::Float64, ::Int64)
Closest candidates are:
  Array{Float64,N} where N(::UndefInitializer, ::Int64) where T at boot.jl:408
  Array{Float64,N} where N(::UndefInitializer, ::Int64, ::Int64) where T at boot.jl:409
  Array{Float64,N} where N(::UndefInitializer, ::Int64, ::Int64, ::Int64) where T at boot.jl:410
  ...
Stacktrace:
 [1] top-level scope at none:0

```

Could someone please show me the correct way to do this?

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [January 18, 2019, 11:04am UTC](https://discourse.julialang.org/t/how-to-create-an-array-with-a-specified-number-and-specified-length/19779/2 "2019-01-18T11:04:23Z")

</div>

You could use `fill()`:

```julia
Julia-1.0.3> fill(430.0, 5)
5-element Array{Float64,1}:
 430.0
 430.0
 430.0
 430.0
 430.0

```

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [January 18, 2019, 11:04am UTC](https://discourse.julialang.org/t/how-to-create-an-array-with-a-specified-number-and-specified-length/19779/3 "2019-01-18T11:04:45Z")

</div>

```julia
c = Vector{Float64}(undef, 5)
fill!(c, 430.0)

```

---

<div class="post-metadata">

**Author:** ![aortizb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aortizb/32/6513_2.png) [@aortizb](https://discourse.julialang.org/u/aortizb)\
**Post date:** [January 18, 2019, 11:16am UTC](https://discourse.julialang.org/t/how-to-create-an-array-with-a-specified-number-and-specified-length/19779/4 "2019-01-18T11:16:27Z")

</div>

Also

```julia
430 * ones(5)

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [January 18, 2019, 12:42pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-with-a-specified-number-and-specified-length/19779/5 "2019-01-18T12:42:34Z")

</div>

A very MATLAB-ish one, but please don’t recommend this to a beginner in Julia.

---

<div class="post-metadata">

**Author:** ![aortizb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aortizb/32/6513_2.png) [@aortizb](https://discourse.julialang.org/u/aortizb)\
**Post date:** [January 18, 2019, 1:28pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-with-a-specified-number-and-specified-length/19779/6 "2019-01-18T13:28:19Z")

</div>

I am not recommending. I said “also.” It is available in Julia, so it is there. Otherwise, it shouldn’t be there.

---

<div class="post-metadata">

**Author:** ![algebrato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algebrato/32/6778_2.png) [@algebrato](https://discourse.julialang.org/u/algebrato)\
**Post date:** [January 18, 2019, 1:46pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-with-a-specified-number-and-specified-length/19779/7 "2019-01-18T13:46:26Z")

</div>

I suggest to use

`c = [430 for i = 1:5]`

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [January 18, 2019, 2:32pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-with-a-specified-number-and-specified-length/19779/8 "2019-01-18T14:32:35Z")

</div>

> [@Seif\_Shebl](#):
>
> A very MATLAB-ish one, but please don’t recommend this to a beginner in Julia.

It would be good to follow this up with a reason. Being MATLAB-ish is not a good reason 🙂

The `ones` solution creates a vector with the values 1.0, then multiplies each entry with 430, storing the result in a new array in the process. This is both slower and allocates more memory than the `fill` solution, and is clearly inferior in terms of performance.

However, for small sporadic arrays, or working in the REPL, this doesn’t really matter, and it is in fact the _shortest_ way I know of to solve the problem, which makes it more convenient to type in the REPL 🙂

```julia
fill(430.,5) # 12 characters
430ones(5) # 10 characters

```

From a language-learning perspective, I also think it’s interesting to see different ways to solve a problem. Here are a few more, all approximately as efficient as `fill` for larger vectors, not as readable, but interesting nonetheless IMO:

```julia
1:5 .|>_-> 430.0

(_->430.0).(1:5)

[430.0 for _=1:5]

map(_->430.0, 1:5)

```

---

<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:** [January 18, 2019, 5:13pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-with-a-specified-number-and-specified-length/19779/9 "2019-01-18T17:13:09Z")

</div>

> [@tlienart](#):
>
> ```julia
> c = Vector{Float64}(undef, 5) 
> fill!(c, 430.0)
> 
> ```

Or,

```julia
c = Vector{Float64}(undef, 5) 
c .= 430

```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 18, 2019, 7:42pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-with-a-specified-number-and-specified-length/19779/10 "2019-01-18T19:42:07Z")

</div>

Playing with benchmarking…

```julia
julia> using BenchmarkTools

julia> @btime fill(430.0,5);
  37.048 ns (1 allocation: 128 bytes)

julia> @btime begin
       c = Vector{Float64}(undef,5)
       fill!(c,430.0)
       end;
  37.198 ns (1 allocation: 128 bytes)

julia> @btime 430.0*ones(5);
  73.867 ns (2 allocations: 256 bytes)

julia> @btime [430.0 for i in 1:5];
  39.206 ns (1 allocation: 128 bytes)

julia> @btime [430.0 for i = 1:5];
  39.051 ns (1 allocation: 128 bytes)

julia> @btime 1:5 .|>_->430.0;
  33.333 ns (1 allocation: 128 bytes)

julia> @btime (_->430.0).(1:5);
  33.836 ns (1 allocation: 128 bytes)

julia> @btime [430.0 for _=1:5];
  39.658 ns (1 allocation: 128 bytes)

julia> @btime map(_->430.0,1:5);
  39.555 ns (1 allocation: 128 bytes)

```
