朱莉娅:将数组矢量转换为任意维度的数组 [英] Julia: Converting Vector of Arrays to Array for Arbitrary Dimensions

查看:196
本文介绍了朱莉娅:将数组矢量转换为任意维度的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用计时测试,我发现使用push!增长Vector{Array{Float64}}对象要比仅使用Array{Float64}对象和hcatvcat增长得多.但是,计算完成后,我需要将结果对象更改为Array{Float64}进行进一步分析.是否有一种不管尺寸大小都可以工作的方法?例如,如果我通过

Using timing tests, I found that it's much more performant to grow Vector{Array{Float64}} objects using push! than it is to simply use an Array{Float64} object and either hcat or vcat. However, after the computation is completed, I need to change the resulting object to an Array{Float64} for further analysis. Is there a way that works regardless of the dimensions? For example, if I generate the Vector of Arrays via

u =  [1 2 3 4
      1 3 3 4
      1 5 6 3
      5 2 3 1]
uFull = Vector{Array{Int}}(0)
push!(uFull,u)
for i = 1:10000
  push!(uFull,u)
end

我可以这样进行转换:

fill = Array{Int}(size(uFull)...,size(u)...)
for i in eachindex(uFull)
  fill[i,:,:] = uFull[i]
end

但是请注意,这需要我知道数组是矩阵(二维).如果是3维的,我将需要另一个:,因此这不适用于任意尺寸.

but notice this requires that I know the arrays are matrices (2-dimensional). If it's 3-dimensional, I would need another :, and so this doesn't work for arbitrary dimensions.

请注意,我还需要一种任意尺寸的逆变换"形式(除了首先由整个数组的最后一个索引索引的索引),而且我目前拥有

Note that I also need a form of the "inverse transform" (except first indexed by the last index of the full array) in arbitrary dimensions, and I currently have

filla = Vector{Array{Int}}(size(fill)[end])
  for i in 1:size(fill)[end]
filla[i] = fill[:,:,i]' 
end

我认为第一次转换的方法也可能会解决第二次转换.

I assume the method for the first conversion will likely solve the second as well.

推荐答案

这是Julia的

This is the sort of thing that Julia's custom array infrastructure excels at. I think the simplest solution here is to actually make a special array type that does this transformation for you:

immutable StackedArray{T,N,A} <: AbstractArray{T,N}
    data::A # A <: AbstractVector{<:AbstractArray{T,N-1}}
    dims::NTuple{N,Int}
end
function StackedArray(vec::AbstractVector)
    @assert all(size(vec[1]) == size(v) for v in vec)
    StackedArray(vec, (length(vec), size(vec[1])...))
end
StackedArray{T, N}(vec::AbstractVector{T}, dims::NTuple{N}) = StackedArray{eltype(T),N,typeof(vec)}(vec, dims)
Base.size(S::StackedArray) = S.dims
@inline function Base.getindex{T,N}(S::StackedArray{T,N}, I::Vararg{Int,N})
    @boundscheck checkbounds(S, I...)
    S.data[I[1]][Base.tail(I)...]
end

现在,只需将向量包装在StackedArray中,它的行为就类似于N + 1维数组.这可以扩展并具有更多功能(它可以类似地支持setindex!甚至push! ing数组以本机连接),但是我认为这足以解决您的问题.通过简单地将uFull包装在StackedArray中,您将获得一个与Array{T, N+1}相似的对象.制作一个copy,您将得到一个密集的Array{T, N+1},而无需自己编写for循环.

Now just wrap your vector in a StackedArray and it'll behave like an N+1 dimensional array. This could be expanded and made more featureful (it could similarly support setindex! or even push!ing arrays to concatenate natively), but I think that it's sufficient to solve your problem. By simply wrapping uFull in a StackedArray you get an object that acts like an Array{T, N+1}. Make a copy, and you get exactly a dense Array{T, N+1} without ever needing to write a for loop yourself.

julia> S = StackedArray(uFull)
10001x4x4 StackedArray{Int64,3,Array{Array{Int64,2},1}}:
[:, :, 1] =
 1  1  1  5
 1  1  1  5
 1  1  1  5
…

julia> squeeze(S[1:1, :, :], 1) == u
true

julia> copy(S) # returns a dense Array{T,N}
10001x4x4 Array{Int64,3}:
[:, :, 1] =
 1  1  1  5
 1  1  1  5
…

最后,我要指出这里还有另一个解决方案:您可以引入自定义数组类型 sooner ,并创建一个GrowableArray,将其元素内部存储为线性Vector{T},但是可以直接推送整个列或数组.

Finally, I'll just note that there's another solution here: you could introduce the custom array type sooner, and make a GrowableArray that internally stores its elements as a linear Vector{T}, but allows pushing entire columns or arrays directly.

这篇关于朱莉娅:将数组矢量转换为任意维度的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆