如何将2D数组(n
- by - m
)转换为长度为m
的{{1}}元组的元组,这些元素对应于数组的列?换句话说,我想要一个函数n
,以便
f
朱莉娅是否提供这样的功能?如果没有,我怎么写一个?
答案 0 :(得分:4)
据我所知,没有内置的Julia功能可以满足您的需求,即
Array{T,2}
),(Array{T,1},Array{T,1},...)
元组。但是,您可以使用
的组合轻松地自己编写一个...
运算符,size
函数,tuple
功能。function f{T<:Any}(A::Array{T,2})
return tuple([A[:,c] for c in 1:size(A,2)]...)
end
julia> A = rand(4,5)
4x5 Array{Float64,2}:
0.21149 0.841894 0.275182 0.33981 0.0142366
0.88409 0.718435 0.368415 0.521676 0.527016
0.482613 0.211109 0.394439 0.141225 0.071393
0.4477 0.330136 0.0303556 0.610213 0.699511
julia> tup = f(A)
([0.21149,0.88409,0.482613,0.4477],[0.841894,0.718435,0.211109,0.330136],[0.275182,0.368415,0.394439,0.0303556],[0.33981,0.521676,0.141225,0.610213],[0.0142366,0.527016,0.071393,0.699511])
julia> typeof(tup)
(Array{Float64,1},Array{Float64,1},Array{Float64,1},Array{Float64,1},Array{Float64,1})