# Reshape a Matrix to a vector

**URL:** <https://discourse.julialang.org/t/reshape-a-matrix-to-a-vector/98042>\
**Category:** New to Julia\
**Created:** [April 28, 2023, 1:53pm UTC](https://discourse.julialang.org/t/reshape-a-matrix-to-a-vector/98042 "2023-04-28T13:53:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gsh19](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gsh19/32/49350_2.png) [@gsh19](https://discourse.julialang.org/u/gsh19)\
**Post date:** [April 28, 2023, 1:53pm UTC](https://discourse.julialang.org/t/reshape-a-matrix-to-a-vector/98042/1 "2023-04-28T13:53:51Z")

</div>

I have a matrix X with dimensions (1000,4). I want to convert this matrix to a vector of vectors such that the size of the resulting vector is (1000,) where each element has the size (4,). I tried using reshape(X, (1000,)) but that does not work. Could you please help. Thanks!

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [April 28, 2023, 2:02pm UTC](https://discourse.julialang.org/t/reshape-a-matrix-to-a-vector/98042/2 "2023-04-28T14:02:48Z")

</div>

Depending on what you want to do with the resulting vector of vectors it would be more idiomatic Julia to use `eachrow` to create an iterator over views of the rows.

```julia
help?> eachrow
search: eachrow searchsorted searchsortedlast searchsortedfirst

  eachrow(A::AbstractVecOrMat)

  Create a generator that iterates over the first dimension of vector or matrix A, returning the rows as AbstractVector
  views.

  See also eachcol, eachslice, mapslices.

  │ Julia 1.1
  │
  │ This function requires at least Julia 1.1.

  Example
  ≡≡≡≡≡≡≡≡≡

  julia> a = [1 2; 3 4]
  2×2 Matrix{Int64}:
   1 2
   3 4
  
  julia> first(eachrow(a))
  2-element view(::Matrix{Int64}, 1, :) with eltype Int64:
   1
   2
  
  julia> collect(eachrow(a))
  2-element Vector{SubArray{Int64, 1, Matrix{Int64}, Tuple{Int64, Base.Slice{Base.OneTo{Int64}}}, true}}:
   [1, 2]
   [3, 4]

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 28, 2023, 2:50pm UTC](https://discourse.julialang.org/t/reshape-a-matrix-to-a-vector/98042/3 "2023-04-28T14:50:28Z")

</div>

> [@gsh19](#):
>
> I have a matrix X with dimensions (1000,4). I want to convert this matrix to a vector of vectors such that the size of the resulting vector is (1000,) where each element has the size (4,).

Dealing with lots of vectors of a small fixed size (e.g. 4) is often a sign that you might be better off using [StaticArrays](https://github.com/JuliaArrays/StaticArrays.jl).

e.g.

```julia
julia> using StaticArrays

julia> A = rand(1000,4);

julia> V = [@SVector[A[i,1], A[i,2], A[i,3], A[i,4]] for i in axes(A,1)]
1000-element Vector{SVector{4, Float64}}:
 [0.4434452477917964, 0.3446253018597002, 0.5674041252593228, 0.7059583131641392]
 [0.19970405217542575, 0.9579823112299621, 0.8512050026856272, 0.09805315719974939]
 ⋮
 [0.15556234340431896, 0.4183665044671866, 0.7501469249778085, 0.5471984014874345]
 [0.3607016689564623, 0.3410541849909612, 0.7761437088965141, 0.5671709603611581]

```

The elements of `V` will be much quicker to work with than a `Vector`, and `V` is also stored more efficiently than an array of `Vector`. (The price is that you need to make sure that the length of the `SVector`s is known to the compiler, as I did above by simply hard-coding the length. If you have a wide variety of short lengths, then this requires more care, and it may not be worth it at all if the lengths of the short vectors changes frequently.)

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [April 28, 2023, 5:21pm UTC](https://discourse.julialang.org/t/reshape-a-matrix-to-a-vector/98042/4 "2023-04-28T17:21:44Z")

</div>

Note also that since Julia is column major, a matix of size `(4,1000)` can be reinterpreted as 1000 static arrays of size 4, but a matrix of size `(1000,4)` can’t.
