Vectorize an Array of Arrays

I have a two-dimensional Array containing two-dimensional arrays of various sizes, something like:

nxm Array{Array{Float64,2},2}:
 zeros(3,3)   ones(3,3)   zeros(3,3)
 ones(2,2)    zeros(2,2)  ones(2,2)

Is there a simple way to vectorize this into an Array{Float64,1}? And more importantly, recover back the nested array with the initial structure?

While just working with non-nested arrays, I could easily use reshape() to vectorise and convert back. Perhaps there is an equivalent? Asking because they are decision variables to an optimizer that only accepts vector inputs.

Thanks in advance:)

Hmmm, did you consider having the data in an Array{Float64,1} (as needed by the solver), and then having your matrix of matrices containing views instead? I have to admit I have never created a matrix view from a Vector (i.e., two-dimensional view from one-dimensional data) but I think this direction is more probable than the opposite (one-dimensional view from multiple two-dimensional data sources).

1 Like

Thank you for this, I wasn’t aware of views! I will try to see if that works on my problem.

1 Like

Any doubt just post here, XD.

1 Like

Thanks! I think I made it work using reshape(view(vector, ...), n, m) as a way to recover the structure of the sub 2D arrays. The optimizer was happy with it!

1 Like

Check out Componentarrays.jl, it does exactly this while also giving each array component a name.

1 Like