当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。