Split 2d array into array of 1d arrays

If you have an array of arrays, you can join them into a 2d array as follows:

a = fill(ones(10), 10)
A = hcat(a...)

How can you do the reverse?

I did this some time ago as well. Have a look here for the code with some community builds.

Thanks. I assumed this was a duplicate question but couldn’t find a relevant thread. In short, the solution is:

[A[:, i] for i in 1:size(A, 2)]

Edit: Thanks @DNF

Since Julia 1.1, we also have eachrow for this.


If the Columns are short, consider reinterpret to reinterpret the matrix as an array off SVectors.

1 Like

The inverse of hcat would be eachcolumn or [A[:, i] for i in 1:size(A,2)].

BTW, reduce(hcat, a) is preferable to splatting for efficiency reasons.