# Converting a matrix into an array of arrays

**URL:** https://discourse.julialang.org/t/converting-a-matrix-into-an-array-of-arrays/17038
**Category:** General Usage
**Tags:** question
**Created:** [November 1, 2018, 7:01am UTC](https://discourse.julialang.org/t/converting-a-matrix-into-an-array-of-arrays/17038 "2018-11-01T07:01:14Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![braamvandyk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/braamvandyk/32/5086_2.png) [@braamvandyk](https://discourse.julialang.org/u/braamvandyk)
#### Post date: [November 1, 2018, 7:01am UTC](https://discourse.julialang.org/t/converting-a-matrix-into-an-array-of-arrays/17038/1 "2018-11-01T07:01:14Z")

</div>

Hi all

I have seen many examples of converting an array of arrays into a matrix, but I would like to go the other way. What I have cobbled together works, but may not be the most elegant or fast solution. I would appreciate any comments and builds.

```
function sliceMatrix(A)
    m, n = size(A)
    B = Array{Array{eltype(A), 1}, 1}(undef, m)

    for i = 1:m
        B[i] = A[i, :]
    end
    return B
end

```

Is there a way to pre-allocate the individual arrays inside B?

Thanks!

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [November 1, 2018, 7:34am UTC](https://discourse.julialang.org/t/converting-a-matrix-into-an-array-of-arrays/17038/2 "2018-11-01T07:34:30Z")

</div>

I don’t think you gain much from preallocating in this situation, but here is one way where I only modified your functions a little bit:

```julia
julia> function slicematrix(A::AbstractMatrix{T}) where T
           m, n = size(A)
           B = Vector{T}[Vector{T}(undef, n) for _ in 1:m]
           for i in 1:m
               B[i] .= A[i, :]
           end
           return B
       end
slicematrix (generic function with 1 method)

julia> slicematrix(rand(2,3))
2-element Array{Array{Float64,1},1}:
 [0.686489, 0.016934, 0.638272]
 [0.317802, 0.543907, 0.208549]

```

but it is probably better and simpler to just define it as:

```julia
function slicematrix(A::AbstractMatrix)
    return [A[i, :] for i in 1:size(A,1)]
end

```

Note also that Julia’s `Array` is column major, so extracting columns (`A[:, i]`) is much faster than extracting rows (`A[i, :]`).

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [November 1, 2018, 8:48am UTC](https://discourse.julialang.org/t/converting-a-matrix-into-an-array-of-arrays/17038/3 "2018-11-01T08:48:39Z")

</div>

A one liner, likely not as efficient as the suggestion above, is  
`mapslices(x->[x], randn(5,5), dims=2)[:]`

---

<div class="post-metadata">

### Author: ![braamvandyk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/braamvandyk/32/5086_2.png) [@braamvandyk](https://discourse.julialang.org/u/braamvandyk)
#### Post date: [November 1, 2018, 6:07pm UTC](https://discourse.julialang.org/t/converting-a-matrix-into-an-array-of-arrays/17038/4 "2018-11-01T18:07:30Z")

</div>

Thank you all! The suggestions are much appreciated.

---

<div class="post-metadata">

### Author: ![MatthijsCox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matthijscox/32/42831_2.png) [@MatthijsCox](https://discourse.julialang.org/u/MatthijsCox)
#### Post date: [March 2, 2020, 1:59pm UTC](https://discourse.julialang.org/t/converting-a-matrix-into-an-array-of-arrays/17038/5 "2020-03-02T13:59:39Z")

</div>

While trying this myself, I found a faster solution than the simple suggestion above.

Iterate over the columns with eachcol or eachrow:

```julia
A = rand(5,10000)
@btime [c[:] for c in eachcol(A)]
@btime [A[:,c] for c in 1:size(A,2)]

julia>
382.300 μs (20005 allocations: 1.75 MiB)
  2.788 ms (38985 allocations: 1.74 MiB)

```

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [March 2, 2020, 9:56pm UTC](https://discourse.julialang.org/t/converting-a-matrix-into-an-array-of-arrays/17038/6 "2020-03-02T21:56:59Z")

</div>

Note that your benchmarking produced false inferences due to accessing global variables. If you interpolate the variables into the expressions (using $). The results are much more similar.  
I also include an even faster version, if you can work with static vectors:

```julia
using StaticArrays
A = rand(5,10000)
@btime [c[:] for c in eachcol($A)]
@btime [$A[:,c] for c in 1:size($A,2)]
@btime vec(reinterpret(SVector{size($A,1),eltype($A)},$A))

  506.052 μs (20005 allocations: 1.75 MiB)
  440.997 μs (10004 allocations: 1.30 MiB)
  1.542 μs (11 allocations: 704 bytes)

```

---

<div class="post-metadata">

### Author: ![BMO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmo/32/14820_2.png) [@BMO](https://discourse.julialang.org/u/BMO)
#### Post date: [May 13, 2020, 10:57am UTC](https://discourse.julialang.org/t/converting-a-matrix-into-an-array-of-arrays/17038/7 "2020-05-13T10:57:42Z")

</div>

I think @MatthijsCox approx with `eachcol` is in fact faster (but still slower than `StaticArrays`), the problem is how columns are collected.

```julia
A = rand(5,10000);
@btime collect(eachcol($A));
@btime [c[:] for c in eachcol($A)];
@btime [$A[:,c] for c in 1:size($A,2)];
[A[:,c] for c in 1:size(A,2)] == collect(eachcol(A))

> julia
  96.849 μs (10004 allocations: 547.00 KiB)
  481.677 μs (20005 allocations: 1.75 MiB)
  413.083 μs (10004 allocations: 1.30 MiB)
  true

```

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [May 13, 2020, 11:11am UTC](https://discourse.julialang.org/t/converting-a-matrix-into-an-array-of-arrays/17038/8 "2020-05-13T11:11:18Z")

</div>

> [@BMO](#):
>
> ```julia
> @btime collect(eachcol($A)); 
> @btime [c[:] for c in eachcol($A)]; 
> @btime [$A[:,c] for c in 1:size($A,2)];
> 
> ```

Note that the latter two create copies, while the first one returns views:

```julia
julia> @btime [c[:] for c in eachcol($A)];
  535.007 μs (20005 allocations: 1.75 MiB)

julia> @btime [c for c in eachcol($A)];
  80.548 μs (10005 allocations: 547.02 KiB)

```
