1

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.

peak
  • 105,803
  • 17
  • 152
  • 177

2 Answers2

2

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
carstenbauer
  • 9,817
  • 1
  • 27
  • 40
1
function array2tuple(a::Array)
   (a...,)
end
Steven Siew
  • 843
  • 8
  • 11