2017-04-21 4 views
3

으로 변환하는 방법 줄리아에서 2D 배열의 Vector로 정의 된 데이터를 Matrix의 2D 배열로 변환하고 싶습니다. 다음 예제에서 설명한 것처럼 데이터 s를 데이터 t로 변환하려고하지만 지금까지 성공하지 못했습니다. 사건을 어떻게 처리해야합니까? 당신은 소스 데이터로 2 차원 배열 또는 1D 배열을 정의하면 배열 {Array {Int64,2}, 1}을 Array {Int64,2}

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 

는 다음의 예와 같이, 나는 그 매트릭스의 2 차원 배열에 성공적으로 바꿀 수 있습니다.

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 
+0

참조 스플래 http://stackoverflow.com/questions/37476815/julia-converting-vector-of-arrays-to-array-for-arbitrary을 사용할 수 있습니다 -dimensions –

답변

10

당신은 VCAT도

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

그게 내가 원하는거야. 감사! @pkofod, –

+3

답변으로 표시해야합니다. –