如何在Julia中将一维数组转换为元组?

问题描述 投票:1回答:2

我想使用reshape函数在Julia中重整形数组,但是新数组的形状本身存储为一维数组。 reshape将元组作为参数,但不作为一维数组。

例如,我希望能够做到这一点:

reshape([1 2 3 ; 4 5 6],(3,2))

但是使用[3,2]而不是(3,2)作为shape参数的输入。将数组[3,2]转换为元组(3,2)似乎很明显,但是,如果无法完成,也许我需要编写另一个reshape函数?

任何建议都值得赞赏。

arrays tuples julia reshape splat
2个回答
1
投票

您可以splat数组:

julia> reshape([1 2 3 ; 4 5 6], [3,2]...)
3×2 Array{Int64,2}:
 1  5
 4  3
 2  6

0
投票
function array2tuple(a::Array)
   (a...,)
end
© www.soinside.com 2019 - 2024. All rights reserved.