How to convert from Array {Array {Int64,2}, 1} to Array {Int64,2}

In Julia, I want to convert data defined as a 2D array vector to a 2D array of a matrix. As described in the following example, I want to convert data s to data t, but so far I have not been successful. How should I deal with this case?

julia> s = [[1 2 3], [4 5 6], [7 8 9]]
3-element Array{Array{Int64,2},1}:
 [1 2 3]
 [4 5 6]
 [7 8 9]

julia> t = [[1 2 3]; [4 5 6]; [7 8 9]]
3××3 Array{Int64,2}:
 1  2  3
 4  5  6
 7  8  9

julia> s |> typeof
Array{Array{Int64,2},1}

julia> t |> typeof
Array{Int64,2}

julia> convert(Array{Int64, 2}, s)
ERROR: MethodError: Cannot `convert` an object of type Array{Array{Int64,2},1} to an object of type Array{Int64,2}
This may have arisen from a call to the constructor Array{Int64,2}(...),
since type constructors fall back to convert methods.

julia> reshape(s, 3, 3)
ERROR: DimensionMismatch("new dimensions (3,3) must be consistent with array size 3")
 in reshape(::Array{Array{Int64,2},1}, ::Tuple{Int64,Int64}) at .\array.jl:113
 in reshape(::Array{Array{Int64,2},1}, ::Int64, ::Int64, ::Vararg{Int64,N}) at .\reshapedarray.jl:39

      

As in the following example, if you define a 2D array or 1D array as raw data, I can successfully convert it to a 2D array of a matrix.

julia> u = [1 2 3 4 5 6 7 8 9]
1××9 Array{Int64,2}:
 1  2  3  4  5  6  7  8  9

julia> u |> typeof
Array{Int64,2}


julia> reshape(u, 3, 3)
3××3 Array{Int64,2}:
 1  4  7
 2  5  8
 3  6  9



julia> v = [1, 2, 3, 4, 5, 6, 7, 8, 9]
9-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6
 7
 8
 9

julia> v  |> typeof
Array{Int64,1}

julia> reshape(v, 3, 3)
3××3 Array{Int64,2}:
 1  4  7
 2  5  8
 3  6  9

      

+3


source to share


1 answer


You can use vcat and splatting



julia> t = vcat(s...)
3×3 Array{Int64,2}:
 1  2  3
 4  5  6
 7  8  9

      

+9


source







All Articles