A = Any[[1,2],[3,4],[5,6]]
function to_matrix(A::Vector{Any})
ncol = length(A)
nrow = length(A[1])
B = Array( typeof(A[1][1]) , (nrow, ncol) )
for i in 1:ncol
for j in 1:nrow
B[j,i] = A[i][j]
end
end
B
end
to_matrix(A)
2x3 Array{Int64,2}:
1 3 5
2 4 6
function use_hcat(A::Vector{Any})
B = A[1]
for i=2:length(A)
B = hcat(B,A[i])
end
B
end
use_hcat(A)
2x3 Array{Int64,2}:
1 3 5
2 4 6
@time to_matrix(A)
elapsed time: 1.1926e-5 seconds (504 bytes allocated)
@time use_hcat(A)
elapsed time: 8.743e-6 seconds (288 bytes allocated)
El viernes, 22 de agosto de 2014 23:05:19 UTC-3, Bradley Setzler escribió:
>
> Good evening,
>
> I often have Any types filled with vectors (especially as the return of a
> pmap), and need them as a matrix or DataFrame. For example, suppose I have,
>
> julia> A
> 3-element Array{Any,1}:
> [1,2]
> [3,4]
> [5,6]
>
> But I want,
>
> julia> B
> 2x3 Array{Int64,2}:
> 1 3 5
> 2 4 6
>
> I came up with the following, which successfully constructs B from A, but
> it's a bit inefficient/messy:
>
> B = A[1]
> for i=2:length(A)
> B = hcat(B,A[i])
> end
>
> Is there a better way to do this? Something like,
> julia> B = hcatForAny(A)
>
> Thanks,
> Bradley
>