gpt4 book ai didi

arrays - 在 Julia 中将 3D 数组转换为 2D 数组的最有效方法是什么?

转载 作者:行者123 更新时间:2023-12-04 00:01:07 25 4
gpt4 key购买 nike

主题说明了一切。

我只是想这样做,但对我来说,分配新数组效率低下。

julia> a = reshape(1:30, 2, 3, 5)

进入

julia> b = [a[:, :, i] for i in 1:5]

有没有一种简单的方法可以将类型从 Array{T, 3} 转换为 Array{Array{T, 2}, 1}

最佳答案

如果没有分配,这是不可能做到的。但是,您也许可以使用 View 而不是实际创建数组,例如通过做

julia> a = reshape(1:30, 2, 3, 5);

julia> collect(eachslice(a, dims=3))
5-element Array{SubArray{Int64,2,Base.ReshapedArray{Int64,3,UnitRange{Int64},Tuple{}},Tuple{Base.Slice{Base.OneTo{Int64}},Base.Slice{Base.OneTo{Int64}},Int64},true},1}:
[1 3 5; 2 4 6]
[7 9 11; 8 10 12]
[13 15 17; 14 16 18]
[19 21 23; 20 22 24]
[25 27 29; 26 28 30]

关于arrays - 在 Julia 中将 3D 数组转换为 2D 数组的最有效方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61452890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com