简体   繁体   中英

How to convert a 1D array to tuple in Julia?

I want to reshape an array in Julia using the reshape function but the shape of the new array is stored as a 1-D array itself. reshape takes tuples as argument but not 1D array.

For example, I want to be able to do this:

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

but using [3,2] instead of (3,2) as the input to the shape parameter. Converting array [3,2] to tuple (3,2) seems like the obvious thing to do, but if that can't be done, maybe I need to write another reshape function?

Any advice is appreciated.

You can splat the array:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM