當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Julia repeat()用法及代碼示例


這個repeat()是 julia 中的一個內置函數,用於通過以指定的次數重複指定的數組元素來構造數組。

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

參數:

  • A::抽象數組:指定數組。
  • 計數::整數:每個元素重複指定的次數。
  • inner:它表示單個元素的重複。
  • outer:它表示整個切片的重複。
  • s::抽象字符串:指定的字符串。
  • r::整數:每個元素重複指定的次數。
  • c::AbstractChar:指定的字符。

返回值:它返回一個新構造的數組。

範例1:




# 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))

輸出:

範例2:


# 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)))

輸出:

範例3:


# 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))

輸出:

GFGGFGGFG
GeeksforGeeksGeeksforGeeks
aaaa
AAAAA



相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Creating array with repeated elements in Julia – repeat() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。