arraystuplesjuliareshapesplat

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.


Solution

  • 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