# Inplace conversion of a (m,n)-array into an m-element array

**URL:** <https://discourse.julialang.org/t/inplace-conversion-of-a-m-n-array-into-an-m-element-array/49319>\
**Category:** New to Julia\
**Created:** [October 30, 2020, 6:09pm UTC](https://discourse.julialang.org/t/inplace-conversion-of-a-m-n-array-into-an-m-element-array/49319 "2020-10-30T18:09:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [October 30, 2020, 6:09pm UTC](https://discourse.julialang.org/t/inplace-conversion-of-a-m-n-array-into-an-m-element-array/49319/1 "2020-10-30T18:09:35Z")

</div>

Hi everyone,

I’m new to Julia, comming from Python. I was a bit confused when defining an array as a list of lists in Python, I noticed that it isn’t an array of size(m,n).  
I searched the WEB and here, for an inplace conversion of an array of size (m,n) to an m-element array, but couldn’t find any example/reference. Is such a conversion possible?  
Example, if I have an array:

```julia
arr = rand(1:15, (3,4))
3×4 Array{Int64,2}:
  6 8 8 13
  4 3 14 6
 14 13 1 12    

```

I need to convert it inplace into:

```julia
[[6, 8, 8, 13],
[4, 3, 14, 6],
[14, 13, 1, 12]]

```

At the moment I defined the associated m-element array by:

```julia
elarr = [arr[k, :] for k in 1:m]    

```

but for a big m and n it is memory consuming to have both `arr` and `elarr` defined.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [October 30, 2020, 6:31pm UTC](https://discourse.julialang.org/t/inplace-conversion-of-a-m-n-array-into-an-m-element-array/49319/2 "2020-10-30T18:31:53Z")

</div>

Julia is column-major, so it’ll be faster to flip your array’s indices and use column-wise slices. Try `collect(eachcol(arr))`, or `eachrow` if you really need the rows for some reason.

```julia
julia> arr = rand(1:15, (3,4))
3×4 Array{Int64,2}:
 9 14 9 1
 5 8 13 5
 4 13 15 4

julia> collect(eachcol(arr))
4-element Array{SubArray{Int64,1,Array{Int64,2},Tuple{Base.Slice{Base.OneTo{Int64}},Int64},true},1}:
 [9, 5, 4]
 [14, 8, 13]
 [9, 13, 15]
 [1, 5, 4]

```

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [October 30, 2020, 8:44pm UTC](https://discourse.julialang.org/t/inplace-conversion-of-a-m-n-array-into-an-m-element-array/49319/3 "2020-10-30T20:44:59Z")

</div>

If you really need it to be in-place (meaning, sharing the same data in RAM), then you should use `reinterpret` in conjunction with StaticArrays.jl:

```julia
julia> arr = rand(1:15, (3,4))
3×4 Matrix{Int64}:
  2 8 14 14
 11 9 6 9
  6 3 5 12

julia> using StaticArrays

julia> av1 = reinterpret(SVector{3,Int}, arr)
1×4 reinterpret(SVector{3, Int64}, ::Matrix{Int64}):
 [2, 11, 6] [8, 9, 3] [14, 6, 5] [14, 9, 12]

```

Because Julia is column-major, each column of `arr` has been turned into a vector. You specify the column size as the first parameter to `SVector`.

On the not-yet-released Julia 1.6, you also have the option to simultaneously reshape into a vector:

```julia
julia> av2 = reinterpret(reshape, SVector{3,Int}, arr) # requires Julia 1.6
4-element reinterpret(reshape, SVector{3, Int64}, ::Matrix{Int64}) with eltype SVector{3, Int64}:
 [2, 11, 6]
 [8, 9, 3]
 [14, 6, 5]
 [14, 9, 12]

```

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [October 30, 2020, 9:13pm UTC](https://discourse.julialang.org/t/inplace-conversion-of-a-m-n-array-into-an-m-element-array/49319/4 "2020-10-30T21:13:59Z")

</div>

My `eachcol` solution should be in-place, unless there’s some nuance I’m missing:

```julia
julia> collect(eachcol(arr))[1][1] = 100
100

julia> arr
3×4 Array{Int64,2}:
 100 14 9 1
   5 8 13 5
   4 13 15 4

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 30, 2020, 9:57pm UTC](https://discourse.julialang.org/t/inplace-conversion-of-a-m-n-array-into-an-m-element-array/49319/5 "2020-10-30T21:57:29Z")

</div>

I think that you are right, but in versions before Julia 1.5 your solution may lead to more allocations (because views may allocate and your alternative generate a view for each column).

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [October 31, 2020, 8:58am UTC](https://discourse.julialang.org/t/inplace-conversion-of-a-m-n-array-into-an-m-element-array/49319/6 "2020-10-31T08:58:20Z")

</div>

@tim.holy

Thank you for your suggestion. I have to dig deeper into StaticArrays, because I’m only 7 days old with Julia. 🙂

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [October 31, 2020, 9:04am UTC](https://discourse.julialang.org/t/inplace-conversion-of-a-m-n-array-into-an-m-element-array/49319/7 "2020-10-31T09:04:43Z")

</div>

You’re right, thanks for the correction. Some people use in-place to mean “no allocation” but your definition is really the right one.
