# Convert array into matrix in place

**URL:** <https://discourse.julialang.org/t/convert-array-into-matrix-in-place/55624>\
**Category:** New to Julia\
**Created:** [February 19, 2021, 3:17pm UTC](https://discourse.julialang.org/t/convert-array-into-matrix-in-place/55624 "2021-02-19T15:17:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon74562486](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@anon74562486](https://discourse.julialang.org/u/anon74562486)\
**Post date:** [February 19, 2021, 3:17pm UTC](https://discourse.julialang.org/t/convert-array-into-matrix-in-place/55624/1 "2021-02-19T15:17:22Z")

</div>

Is there any way to convert an array into a matrix in place?  
Thank you.

---

<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:** [February 19, 2021, 3:25pm UTC](https://discourse.julialang.org/t/convert-array-into-matrix-in-place/55624/2 "2021-02-19T15:25:58Z")

</div>

> [@anon74562486](#):
>
> Is there any way to convert an array into a matrix in place?

A matrix is a type of array. Do you mean convert a 1d array (a “vector”) into a 2d array (a “matrix”)? Use `reshape`:

```julia
julia> v = [0:24;]
25-element Vector{Int64}:
  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24

julia> reshape(v, 5,5)
5×5 Matrix{Int64}:
 0 5 10 15 20
 1 6 11 16 21
 2 7 12 17 22
 3 8 13 18 23
 4 9 14 19 24

```

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [February 19, 2021, 3:39pm UTC](https://discourse.julialang.org/t/convert-array-into-matrix-in-place/55624/3 "2021-02-19T15:39:02Z")

</div>

To complete the answer of stevengj, a `Matrix{T}` is nothing else than an alias for `Array{T,2}`, as `Vector{T}` is nothing else than an alias for `Array{T,1}`

---

<div class="post-metadata">

**Author:** ![anon74562486](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@anon74562486](https://discourse.julialang.org/u/anon74562486)\
**Post date:** [February 19, 2021, 3:42pm UTC](https://discourse.julialang.org/t/convert-array-into-matrix-in-place/55624/4 "2021-02-19T15:42:24Z")

</div>

Thank you, but I noticed that using reshape function in a multi threading for loop eats too much memory.  
Are there in place alternatives?

---

<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:** [February 19, 2021, 3:59pm UTC](https://discourse.julialang.org/t/convert-array-into-matrix-in-place/55624/5 "2021-02-19T15:59:16Z")

</div>

`reshape` is in-place:

```julia
julia> v = rand(10^6);

julia> using BenchmarkTools

julia> @btime reshape($v, 1000,1000);
  42.719 ns (2 allocations: 96 bytes)

```

Notice that the allocation here is quite small and is essentially just a wrapper object.

You can actually use an _explicit_ wrapper object of type `Base.ReshapedArray` with some undocumented internals:

```julia
julia> reshape2(v, dims) = Base.__reshape((v,IndexLinear()), (1000,1000))
reshape2 (generic function with 1 method)

julia> v = rand(10^6);

julia> @btime reshape2($v, (1000,1000));
  2.034 ns (0 allocations: 0 bytes)

```

(Beware that the above `reshape2` function doesn’t do any argument checking, e.g. it doesn’t check whether the sizes are compatible.)

See also the discussion in [reshape(::Array, ...) is slow and allocates · Issue #36313 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/36313)

---

<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:** [February 19, 2021, 4:06pm UTC](https://discourse.julialang.org/t/convert-array-into-matrix-in-place/55624/6 "2021-02-19T16:06:34Z")

</div>

> [@stevengj](#):
>
> (Beware that the above `reshape2` function doesn’t do any argument checking, e.g. it doesn’t check whether the sizes are compatible.)

A safer version (with basically the same performance, but doing argument checking) should be:

```julia
reshape2(a, dims) = invoke(Base._reshape, Tuple{AbstractArray,typeof(dims)}, a, dims)

```

---

<div class="post-metadata">

**Author:** ![kbarros](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kbarros/32/7723_2.png) [@kbarros](https://discourse.julialang.org/u/kbarros)\
**Post date:** [February 19, 2021, 7:37pm UTC](https://discourse.julialang.org/t/convert-array-into-matrix-in-place/55624/7 "2021-02-19T19:37:54Z")

</div>

Does this work?

```julia
reshape(view(v, :), 1000, 1000)

```

Inserting the “view” first seems to avoid all heap allocations.
