kkb001
kkb001

Reputation: 511

Repeat an integer in julia

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?

Upvotes: 5

Views: 1392

Answers (3)

Erwin Kalvelagen
Erwin Kalvelagen

Reputation: 16797

What is wrong with:

i = 4
repeat([i],6)

It gives:

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

Upvotes: 1

Korsbo
Korsbo

Reputation: 724

I think you may be looking for the fill function.

i = 4
nr_repeats = 10

fill(i, nr_repeats)

Upvotes: 1

hckr
hckr

Reputation: 5583

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

Upvotes: 6

Related Questions