Creating array with repeated elements in Julia – repeat() Method

The repeat() is an inbuilt function in julia which is used to construct an array by repeating the specified array elements with the specified number of times.

Syntax:
repeat(A::AbstractArray, counts::Integer…)
or
repeat(A::AbstractArray; inner, outer)
or
repeat(s::AbstractString, r::Integer)
or
repeat(c::AbstractChar, r::Integer)

Parameters:

  • A::AbstractArray: Specified array.
  • counts::Integer: Specified number of times each element get repeated.
  • inner: It says the repetition of individual element.
  • outer: It says the repetition of whole slice.
  • s::AbstractString: Specified string.
  • r::Integer: Specified number of times each element get repeated.
  • c::AbstractChar: Specified character.

Returns: It returns a new constructed array.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Julia program to illustrate 
# the use of Array repeat() method
  
# Constructing an array by repeating
# the specified 1D array with the 
# specified number of times.
A = [1, 2, 3, 4];
println(repeat(A, 1))
  
# Constructing an array by repeating
# the specified 1D array with the 
# specified number of times.
B = [1, 2, 3, 4];
println(repeat(B, 1, 2))
  
# Constructing an array by repeating
# the specified 2D array with the 
# specified number of times.
C = [1 2; 3 4];
println(repeat(C, 2))
  
# Constructing an array by repeating
# the specified 2D array with the 
# specified number of times.
D = [1 2; 3 4];
println(repeat(D, 2, 2))

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Julia program to illustrate 
# the use of Array repeat() method
  
# Constructing an array by repeating
# the specified elements with the 
# specified inner value
println(repeat(1:3, inner = 2))
  
# Constructing an array by repeating
# the specified elements with the 
# specified outer value
println(repeat(2:4, outer = 2))
  
# Constructing an array by repeating
# the specified elements with the 
# specified inner and outer value
println(repeat([5 10; 15 20], inner =(2, 1), outer =(1, 3)))

chevron_right


Output:

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Julia program to illustrate 
# the use of Array repeat() method
  
# Getting new string after repetition of
# specified string or character
println(repeat("GFG", 3))
println(repeat("GeeksforGeeks", 2))
println(repeat("a", 4))
println(repeat("A", 5))

chevron_right


Output:

GFGGFGGFG
GeeksforGeeksGeeksforGeeks
aaaa
AAAAA



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.