JuliaJulia语言

Julia快速入门(四)数组生成、repeat; 错误处理

2018-11-20  本文已影响67人  Julia语言

本篇代码在Julia1.0.2中测试通过

数组

数组定义方法

function printsum(a)
    # summary方法会得到一个对象的概要
    println(summary(a), ": ", repr(a))
end

# 数组可以直接定义
a1 = [1,2,3]
printsum(a1)
# 程序输出: 3-element Array{Int64,1}: [1, 2, 3]

# 也可以直接定义空数组
a2 = []
printsum(a2)
# 程序输出: 0-element Array{Any,1}: Any[]

# 由于上述的定义方法没有元素类型,如push!这样的函数并不能使用
# 所以有时候我们需要声明数组的元素类型
a3 = Int64[]
printsum(a3)
# 程序输出: 0-element Array{Int64,1}: []

数组的特殊生成方法

# ranges的用法和数组有区别
a4 = 1:20
printsum(a4)
# 程序输出: 20-element UnitRange{Int64}: 1:20

# 这样生成数组也是可以的
a4 = collect(1:20)
printsum(a4)
# 程序输出: 20-element Array{Int64,1}: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

# 数组也可以使用comprehensions来生成
a5 = [2^i for i = 1:10]
printsum(a5)
# 程序输出: 10-element Array{Int64,1}: [2,4,8,16,32,64,128,256,512,1024]

# 数组元素可以是任意类型,所以数组的数组是合法滴
a6 = (Array{Int64, 1})[]
printsum(a6)
# 程序输出: 0-element Array{Array{Int64,1},1}: []
# (注意这是个嵌套数组,和多维数组是有区别的)

数组的操作函数

a1 = [1,2,3]
a3 = Int64[]
a6 = (Array{Int64, 1})[]

# Julia提供了很多"Dequeue"函数,其中用的最多的就是push!方法
# 函数名末尾加一个!说明函数的第一个参数会被更新

push!(a1, 4)
printsum(a1)
# 程序输出: 4-element Array{Int64,1}: [1, 2, 3, 4]

# push!(a2, 1)这种用法将会报错

push!(a3, 1)
printsum(a3) 
# 程序输出: 1-element Array{Int64,1}: [1]

push!(a6, [1,2,3])
printsum(a6)
# 程序输出: 1-element Array{Array{Int64,1},1}: Array{Int64,1}[[1, 2, 3]]

# 在使用repeat()方法时,你必须指定参数"inner"和"outer"
# 参数必须是数组而不能是ranges
a7 = repeat(a1,inner=[2],outer=[1])
printsum(a7)
# 程序输出: 8-element Array{Int64,1}: [1, 1, 2, 2, 3, 3, 4, 4]

a8 = repeat([4:-1:1],inner=[1],outer=[2])
printsum(a8)
# 程序输出: 2-element Array{StepRange{Int64,Int64},1}: StepRange{Int64,Int64}[4:-1:1, 4:-1:1]

数组的repeat函数

repeat(A::AbstractArray, counts::Integer...)
Construct an array by repeating array A a given number of times in each dimension, specified by counts.
在各个维度上重复若干次

julia> repeat([1, 2, 3], 2) 
#在第一个维度,即按行的方向(类似numpy中的axis=0,即从上到下),重复2次
6-element Array{Int64,1}:
 1
 2
 3
 1
 2
 3

julia> repeat([1, 2, 3], 2, 3)
#在第一个维度,即按行的方向(类似numpy中的axis=0,即从上到下),重复2次
#在第二个维度,即按列的方向(类似numpy中的axis=1,即从左到右),重复3次
6×3 Array{Int64,2}:
 1  1  1
 2  2  2
 3  3  3
 1  1  1
 2  2  2
 3  3  3

repeat(A::AbstractArray; inner=ntuple(x->1, ndims(A)), outer=ntuple(x->1, ndims(A)))
Construct an array by repeating the entries of A. The i-th element of inner specifies the number of times that the individual entries of the i-th dimension of A should be repeated. The i-th element of outer specifies the number of times that a slice along the i-th dimension of A should be repeated. If inner or outer are omitted, no repetition is performed.
通过重复A的构造数组

julia> repeat(1:2, inner=2)
4-element Array{Int64,1}:
 1
 1
 2
 2

julia> repeat(1:2, outer=2)
4-element Array{Int64,1}:
 1
 2
 1
 2

julia> repeat([1 2; 3 4], inner=(2, 1)) #行的方向重复2次,列的方向重复1次(即不重复)
4×2 Array{Int64,2}:
 1  2
 1  2
 3  4
 3  4

julia> repeat([1 2; 3 4], outer=(1, 3)) # #行的方向重复1次(即不重复),列的方向重复3次
2×6 Array{Int64,2}:
 1  2  1  2  1  2
 3  4  3  4  3  4

julia> repeat([1 2; 3 4], inner=(2, 1), outer=(1, 3))
4×6 Array{Int64,2}:
 1  2  1  2  1  2
 1  2  1  2  1  2
 3  4  3  4  3  4
 3  4  3  4  3  4

错误处理

a=[]
# 和很多语言一样,Julia使用try, catch来完成错误处理过程
try
    push!(a,1)
catch err
    showerror(STDOUT, err, backtrace());println() # Julia1.0.2中有bug,STDOUT没有定义
end
println("Continuing after error")
欢迎关注微信公众账号Julia语言.jpg

点击阅读原文可查看历史文章

上一篇 下一篇

猜你喜欢

热点阅读