How to convert Array/Vector{Array{Float64,2}} into an Array{Float64,2} OR how to create plots from them?

Hello, I would like to know how to convert from an Array/Vector{Array{Float64,2}} into Array{Float64,2} or how I can create a plot from them.

I can convert a

Vector{Array{Float64,2}} with 50001 elements
1×8 Array{Float64,2}:
1×8 Array{Float64,2}:
...

into a

50001×1 Array{Array{Float64,2},2}:
 [0.0 0.711225 … 0.0013899305857990158 -6.671582721176745e-7]
 [1.0 0.7113210329556476 … 0.0013897332870554818 -6.671582721176745e-7]
...

But what I’d prefer is a 50001×8 Array{Float64,2}: as I can then convert it to a DataFrame and
I know how to plot from there. I’ve look at documentation and googled, but I can’t find anything. I apologize if this is something obvious.

Hi! How about this?

a = [Matrix([rand() for i=1:8]') for j=1:3] # Array{Array{Float64,2},1}
b = reduce(vcat,a) # Array{Float64,2}
1 Like

Wonderful!

Do you have advice on how to search for answers like this in the documentation?

See also VectorOfArray from https://github.com/SciML/RecursiveArrayTools.jl

Unfortunately, this a little tricky to find, because it combines two functions/concepts:

  1. Vertical array concatenation with vcat https://docs.julialang.org/en/v1/base/arrays/#Concatenation-and-permutation-1
    There, you find the solution b = vcat(a...)
    This works in your scenario, but can be slow if a has a large number of elements.
  2. The reduce function. It is not easy to find in your context, because it is very generic and is used for many things in functional programming. Its one of these functions that is worth always keeping in mind when using Julia.

I feel the documentation of cat could reference reduce, because you’re not the first person to ask this…

1 Like

Not relevant to the question, but

julia> rand(1,8)
1×8 Array{Float64,2}:
 0.466874  0.26308  0.105319  0.947792  0.916321  0.975012  0.959983  0.361349

is simpler.

1 Like

Two other options; choose first for speed or second for compactness:

julia> a = [rand(1,8) for j=1:3]
3-element Array{Array{Float64,2},1}:
 [0.10125963142469918 0.034107265019388766 … 0.62765959243681 0.5979593698724655]
 [0.9642252312080619 0.6314535023806402 … 0.7191877977658307 0.13816813789137528]
 [0.07586919612074938 0.36551814184760767 … 0.13870916326032479 0.85237811990861]

julia> [a[i][j] for i = 1:3, j = 1:8]
3×8 Array{Float64,2}:
 0.10126    0.0341073  0.846837  0.194128  0.119424  0.327054  0.62766   0.597959
 0.964225   0.631454   0.245285  0.64749   0.682603  0.914577  0.719188  0.138168
 0.0758692  0.365518   0.801229  0.561955  0.366092  0.407825  0.138709  0.852378

julia> vcat(a...)
3×8 Array{Float64,2}:
 0.10126    0.0341073  0.846837  0.194128  0.119424  0.327054  0.62766   0.597959
 0.964225   0.631454   0.245285  0.64749   0.682603  0.914577  0.719188  0.138168
 0.0758692  0.365518   0.801229  0.561955  0.366092  0.407825  0.138709  0.852378