用Julia也有一段时间,从来没有认真地学习一下Array矩阵操作,这个和MATLAB中不太一样,MATLAB本身就是以矩阵为中心,而JULIA还有Dict等非常丰富的collection.
以下可以体会一下,Julia功能的强大,也可以看到,Julia从Matlab那里学了很多,很多功能很相似,而Julia的官方也承认了这点,当然也有一些不同。
一、功能可以容下所有各种类型julia> data =Array(Any,2,5) # 类似于matlab: double(2,5)
2x5 Array{Any,2}:
#undef #undef #undef #undef #undef
#undef #undef #undef #undef #undef
julia> data =Array(Any,2)
2-element Array{Any,1}:
#undef
#undef
julia> data =Array(Any,2,3,4) #多元组
2x3x4 Array{Any,3}:
[:, :, 1] =
#undef #undef #undef
#undef #undef #undef
[:, :, 2] =
#undef #undef #undef
#undef #undef #undef
[:, :, 3] =
#undef #undef #undef
#undef #undef #undef
[:, :, 4] =
#undef #undef #undef
#undef #undef #undef
# 其它类型或形式的Array
julia> Array(Int64,2,5)
2x5 Array{Int64,2}:
0 0 0 0 0
0 0 0 0 0
julia> Array(Float64,2,5) # 等同于Matlab double(2,5)
2x5 Array{Float64,2}:
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
julia> ones(2,5) # 等同于MATLAB ones(2,5)
2x5 Array{Float64,2}:
1.0 1.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0 1.0
julia> zeros(2,5) # 和MATLAB没有区别
2x5 Array{Float64,2}:
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
当然,Array{}里面装Array,Dict,或自己定义的Type等,那完全是没有问题的。所以,这个功能比MATLAB强大。这个带来的好处,不言自明,比如,读csv文件时,不象MATLAB要分二张表来装内容!,Array可以象数据库的表一样,展示给你,这种友好性是MATLAB无法比较的。当然,Matlab也有dataset(),但效率太低,数据量一大,运行起来就会吃力。
#赋值:其实和MATLAB很相似
julia> data =Array(Any,2,3) # Array赋值举例
2x3 Array{Any,2}:
#undef #undef #undef
#undef #undef #undef
julia> data[1,:]=["a" "b" "c"]
1x3 Array{ASCIIString,2}:"a" "b" "c"
julia> data[2,:]=[1 2 "d"] # Array的强大在于可以不限内容,有点象Matlab的cell,
1x3 Array{Any,2}:
1 2 "d"
julia> data
2x3 Array{Any,2}:
"a" "b" "c"
1 2 "d"
# 和MATLAB很不一样的地方
julia> dat =Float64[2,2] # 和MATLAB的double(2,2)完全不一样!
2-element Array{Float64,1}:
2.0
2.0
julia> dat =Array(Float64,2,2) # 这才是和和MATLAB的double(2,2)等效的!
2x2 Array{Float64,2}:
0.0 0.0
0.0 0.0
# 来看看hcat,vcat:和MATLAB超级象
julia>
elapsed time: 10.954116426 seconds (823614756 bytes allocated, 1.92% gc time)
julia> (row,col)=size(csv)
(307458,9)
julia> typeof(csv)
Array{Any,2}
julia> colData=csv[:,2];
julia> @time newcsv =hcat(csv,colData,colData,colData)
elapsed time: 0.177521262 seconds (32235888 bytes allocated)
307458x12 Array{Any,2}:
"Code" "DateTime" … "DateTime" "DateTime" .....
"SFIF0001" 734244.0 734244.0 734244.0
"SFIF0001" 734244.0 734244.0 734244.0
"SFIF0001" 734244.0 734244.0 734244.0
"SFIF0001" 734244.0 734244.0 734244.0
"SFIF0001" 734244.0 .... 734244.0 734244.0 .....
.......
仅管hcat效率不错,但和MATLAB类似,vcat效率也比较低。建议慎用。