Background: My simulation program returns a 4x4 matrix. Running many replicates using
map() or a vector comprehension, I get a long vector of small matrices. Similar to
[rand(4, 4) for _ in 1:10000].
Question: Is there a one-line way to turn a vector of 10,000 small (4x4) matrices into a 3D array (size 4x4x1000)? If the vector was short, one could do:
cat(vector_of_vectors..., dims=3). But that fails for longer vectors as pointed out by others before.
The analogous problem of turning a vector of vectors into a matrix is easy:
For my matrices, I tried
reduce(cat, vector_of_matrices) and
reduce(x -> cat(x, dims=3), vector_of_matrices) without success. At the moment, I use this function, which works and is fast (
mv being the vector of matrices):
function matVec2Arr(mv) a = zeros(size(mv)..., length(mv)) for i in 1:length(mv) a[:,:,i] = mv[i] end return a end
I am just wondering is there a more ‘elegant’ way?