Julia程序设计3 数组1 创建、初始化、属性与访问
Julia程序設計3 數(shù)組
- 創(chuàng)建數(shù)組
- 數(shù)組的屬性
- 訪問數(shù)組中的元素
創(chuàng)建數(shù)組
1、按列創(chuàng)建數(shù)組:如果用中括號創(chuàng)建數(shù)組、元素之間用逗號隔開,創(chuàng)建出來的數(shù)組就是列向量的形式;用中括號也可以按列創(chuàng)建多維數(shù)組
julia> a = [1,2,3,4] 4-element Array{Int64,1}:1234julia> b = [[1,2,3] [4,5,6] [7,8,9]] 3×3 Array{Int64,2}:1 4 72 5 83 6 92、按行創(chuàng)建數(shù)組:如果用中括號創(chuàng)建數(shù)組、元素之間用空格隔開,創(chuàng)建出來的數(shù)組就是行向量的形式;
julia> b = [1 2 3 4] 1×4 Array{Int64,2}:1 2 3 4但這兩種方法創(chuàng)建的數(shù)組維數(shù)是不同的,前一種是一維的后一種是二維的。用size()可以返回數(shù)組的行列數(shù)、ndims()返回數(shù)組的維數(shù):
julia> size(a) (4,)julia> size(b) (1, 4)julia> ndims(a) 1julia> ndims(b) 23、用Array{data_type, Number}(undef,Number)語句創(chuàng)建數(shù)組,Number需要是Int64,{}中的數(shù)字表示數(shù)組的維數(shù),()中的數(shù)字表示數(shù)組的size,size和維數(shù)要統(tǒng)一,比如二維數(shù)組需要兩個數(shù)字表示size,用這個語句創(chuàng)建的數(shù)組會自動用隨機數(shù)填充:
julia> a = Array{Int16}(undef,3) 3-element Array{Int16,1}:1200julia> a = Array{Int16,2}(undef,2,3) 2×3 Array{Int16,2}:31712 0 -276325687 0 52644、用zeros(data_type, Number, Number)語句創(chuàng)建元素全為0的數(shù)組,數(shù)字表示數(shù)組的size;
用ones(data_type, Number, Number)語句創(chuàng)建元素全為0的數(shù)組,數(shù)字同樣表示數(shù)組的size;
用trues( Number, Number)語句創(chuàng)建元素全為true的數(shù)組,數(shù)字表示數(shù)組的size,因為true是Bool型的,無需指定數(shù)據(jù)類型,類似的還有falses( Number, Number)語句
5、用rand(data_type,Number,Number)可以創(chuàng)建[0,1)間的均勻分布的隨機數(shù)組,數(shù)字表示數(shù)組的size;用randn(data_type,Number,Number)可以創(chuàng)建標準正態(tài)分布的隨機數(shù)組,數(shù)字表示數(shù)組的size;
julia> d1 = rand(Float64,3,3) 3×3 Array{Float64,2}:0.110029 0.772143 0.4346260.821364 0.592196 0.6451990.1322 0.433895 0.0889052julia> d2 = randn(Float64,3,3) 3×3 Array{Float64,2}:0.779424 1.00182 0.0407997-1.65429 0.502965 -0.554261.35664 -1.0813 0.8691166、用語句fill!(A,x)可以用某個值xxx填充整個數(shù)組AAA;用fill(x,Number,Number)語句可以創(chuàng)建一個所有值都是xxx,Number決定size的數(shù)組;前者填充后數(shù)組的數(shù)據(jù)類型由AAA決定,后者填充后數(shù)組的數(shù)據(jù)類型由xxx決定
julia> e1 = fill!(d1,1) 3×3 Array{Float64,2}:1.0 1.0 1.01.0 1.0 1.01.0 1.0 1.0julia> e2 = fill(0,3,3) 3×3 Array{Int64,2}:0 0 00 0 00 0 07、基于已有的數(shù)組AAA創(chuàng)建新數(shù)組,可以選擇reshape、copy、similar方法:
julia> A = randn(Float64,4,4) 4×4 Array{Float64,2}:-0.380822 0.0876638 -0.560772 0.152027-0.970889 -1.00651 1.78672 0.3097960.802797 0.868499 -0.401544 1.23172-0.423717 1.29024 1.45242 -0.703994julia> B = copy(A) 4×4 Array{Float64,2}:-0.380822 0.0876638 -0.560772 0.152027-0.970889 -1.00651 1.78672 0.3097960.802797 0.868499 -0.401544 1.23172-0.423717 1.29024 1.45242 -0.703994julia> reshape(A,8,2) 8×2 Array{Float64,2}:-0.380822 -0.560772-0.970889 1.786720.802797 -0.401544-0.423717 1.452420.0876638 0.152027-1.00651 0.3097960.868499 1.231721.29024 -0.703994julia> similar(A) 4×4 Array{Float64,2}:0.0 0.0 0.0 0.00.0 0.0 0.0 0.00.0 0.0 0.0 0.00.0 0.0 0.0 0.0copy方法比較簡單,就是直接復制數(shù)組AAA;similar方法是創(chuàng)建一個數(shù)值類型、維數(shù)、size與AAA相同的未初始化的數(shù)組;reshape是把數(shù)組AAA變成另外的形狀,需要注意的是新的size下元素數(shù)目與AAA要相同,否則會報錯,另外就是reshape是把AAA按列拉平然后按列填充到新的size的數(shù)組內(nèi)的
julia> reshape(A,2,2) ERROR: DimensionMismatch("new dimensions (2, 2) must be consistent with array size 16") Stacktrace:[1] (::Base.var"#throw_dmrsa#196")(::Tuple{Int64,Int64}, ::Int64) at .\reshapedarray.jl:41[2] reshape at .\reshapedarray.jl:45 [inlined][3] reshape(::Array{Float64,2}, ::Int64, ::Int64) at .\reshapedarray.jl:116[4] top-level scope at none:08、創(chuàng)建函數(shù)的數(shù)組:數(shù)組的元素并不一定是數(shù)值,也可以是函數(shù),比如
julia> a = [sin,cos,tan] 3-element Array{Function,1}:sincostanjulia> a[1](pi) 1.2246467991473532e-169、數(shù)組中的元素也不一定具有相同的類型,比如
julia> a = [sin,pi,2.2,4] 4-element Array{Any,1}:sinπ2.24julia> a[1](a[2]) 1.2246467991473532e-1610、創(chuàng)建從aaa到bbb步長為sss的數(shù)組可以用collect(a:s:b),其中a\s\b可以不是整數(shù);省略號可以代替collect函數(shù),比如collect(a:s:b)語句可以用[a:s:b…]代替
julia> collect(1:2:9) 5-element Array{Int64,1}:13579julia> collect(1.2:0.2:3.3) 11-element Array{Float64,1}:1.21.41.61.82.02.22.42.62.83.03.2julia> [1.2:0.2:3.3...] 11-element Array{Float64,1}:1.21.41.61.82.02.22.42.62.83.03.2數(shù)組的屬性
前五個都比較好理解,這里解釋一下后五個。axes方法是返回數(shù)組的指標特征,比如
數(shù)組A維數(shù)是2,size分別為3,4,Base.OneTo()表示指標是從1到括號內(nèi)的數(shù)字,axes(A,1)返回Base.OneTo(3)表示A的行的指標是從1到3。eachindex也是返回數(shù)組的指標特征,但它是把數(shù)組元素按列排的指標,A有12個元素,所以指標是1到12
julia> eachindex(A) Base.OneTo(12)stride方法返回A某一維相鄰元素的指標距離,比如A的每一行相鄰兩個元素指標差異為1,因此stride(A,1)=1;A的每一列相鄰兩個元素指標差異為列數(shù)減1,這里A的列數(shù)為4,因此stride(A,2)=3
julia> stride(A,2) 3julia> stride(A,1) 1訪問數(shù)組中的元素
julia> A = rand(Float64,3,3) 3×3 Array{Float64,2}:0.934257 0.728261 0.1659610.55983 0.681753 0.2370820.922818 0.251498 0.619831julia> A[1,2] 0.7282612277314509julia> A[1:3,1] # 訪問A的第一列 3-element Array{Float64,1}:0.93425672170987870.55982989246319330.9228178819973683julia> c = [1,3]; julia> A[c,:] #訪問A的第1、3行 2×3 Array{Float64,2}:0.934257 0.728261 0.1659610.922818 0.251498 0.619831julia> A[:,c] #訪問A的第1、3列 3×2 Array{Float64,2}:0.934257 0.1659610.55983 0.2370820.922818 0.619831julia> A[end,1] #end用在某個維度表示該維度最后一個元素 0.9228178819973683julia> A[end] #只有一個end表示按列拉平后的最后一個元素 0.6198311712639313 julia> A[end,end] 0.6198311712639313總結
以上是生活随笔為你收集整理的Julia程序设计3 数组1 创建、初始化、属性与访问的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UA MATH564 概率论VI 数理统
- 下一篇: Julia程序设计3 数组2 排序、复制