您可以使用泼溅(...
) and hcat
得到你想要的东西:
julia> a = Vector[[0.8681299566762923,-0.3472589826095631], [3.2300860990307445,3.3731249077464946]]
2-element Array{Array{T,1},1}:
[0.8681299566762923,-0.3472589826095631]
[3.2300860990307445,3.3731249077464946]
julia> hcat(a...)
2x2 Array{Float64,2}:
0.86813 3.23009
-0.347259 3.37312
或者,如果您希望将堆栈停止为行而不是列,您可以执行以下操作:
julia> vcat(map(x->x', a)...)
2x2 Array{Float64,2}:
0.86813 -0.347259
3.23009 3.37312
我不建议建立一个Matrix
逐行,因为这与朱莉娅的冲突列主阵列布局。对于较大的矩阵,将其堆叠为列并转置输出实际上更有效:
julia> a2 = Vector{Float64}[rand(10) for i=1:5000];
julia> stackrows1{T}(a::Vector{Vector{T}}) = vcat(map(transpose, a)...)::Matrix{T}
stackrows1 (generic function with 2 methods)
julia> stackrows2{T}(a::Vector{Vector{T}}) = hcat(a...)'::Matrix{T}
stackrows2 (generic function with 2 methods)
julia> stackrows1(a2) == stackrows2(a2) # run once to compile and make sure functions do the same thing
true
julia> @time for i=1:100 stackrows1(a2); end
elapsed time: 0.142792896 seconds (149 MB allocated, 7.85% gc time in 7 pauses with 0 full sweep)
julia> @time for i=1:100 stackrows2(a2); end
elapsed time: 0.05213114 seconds (88 MB allocated, 12.60% gc time in 4 pauses with 0 full sweep)