A "Julian" way of repeating elements in a given array

Viewed 52

Consider an array as the following:

[1,4,5,9]

The goal is to duplicate each value and append it next to it's copy in Julia, hence:

[1,1,4,4,5,5,9,9]

Is there a neat "julian" way of doing this avoiding a for- loop?

1 Answers

You can use the repeat function:

julia> v = [1, 4, 5, 9]
4-element Vector{Int64}:
 1
 4
 5
 9

julia> repeat(v; inner=2)
8-element Vector{Int64}:
 1
 1
 4
 4
 5
 5
 9
 9
Related