2
votes

The repeat function in julia is used to replicate a vector a number of specified times. What if I want to repeat an integer instead of a vector?

For example,

repeat([1,2,3],3)

gives

9-element Array{Int64,1}: 1 2 3 1 2 3 1 2 3

What I want now is to repeat an integer many times. How is this possible?

3

3 Answers

6
votes

If you want a Vector, you can simply use fill. fill(x, dims) creates an array filled with the value x.

julia> fill(3, 5)
5-element Array{Int64,1}:
 3
 3
 3
 3
 3
1
votes

I think you may be looking for the fill function.

i = 4
nr_repeats = 10

fill(i, nr_repeats)
1
votes

What is wrong with:

i = 4
repeat([i],6)

It gives:

6-element Array{Int64,1}:
 4
 4
 4
 4
 4
 4