# Possible to convert ND arrays to nested arrays "in place"?

**URL:** <https://discourse.julialang.org/t/possible-to-convert-nd-arrays-to-nested-arrays-in-place/78968>\
**Category:** New to Julia\
**Created:** [April 3, 2022, 11:46pm UTC](https://discourse.julialang.org/t/possible-to-convert-nd-arrays-to-nested-arrays-in-place/78968 "2022-04-03T23:46:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinml](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jinml](https://discourse.julialang.org/u/jinml)\
**Post date:** [April 3, 2022, 11:46pm UTC](https://discourse.julialang.org/t/possible-to-convert-nd-arrays-to-nested-arrays-in-place/78968/1 "2022-04-03T23:46:50Z")

</div>

I know I can reinterpret an array of `SVector` as an ND array as follows:

```julia
julia> SA=[@SVector rand(2) for _=1:100];

julia> A=reinterpret(reshape,Float64,SA);

julia> @show SA[1],A[:,1];
(SA[1], A[:, 1]) = ([0.01565087031449286, 0.4977468415798615], [0.01565087031449286, 0.4977468415798615])

```

I can even modify the `SVector` through the reinterpretation:

```julia
julia> A[:,1].=0.0;

julia> @show SA[1],A[:,1];
(SA[1], A[:, 1]) = ([0.0, 0.0], [0.0, 0.0])

```

My question is, given a 2D array like `A` above, is there a way to convert to a 1D array of `SVector`s in-place, like `SA` above, i.e., sharing the same memory ?

---

<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 4, 2022, 12:00am UTC](https://discourse.julialang.org/t/possible-to-convert-nd-arrays-to-nested-arrays-in-place/78968/2 "2022-04-04T00:00:01Z")

</div>

> [@jinml](#):
>
> My question is, given a 2D array like `A` above, is there a way to convert to a 1D array of `SVector` s in-place, like `SA` above, i.e., sharing the same memory

Just use `reinterpret`:

```julia
julia> A = rand(3,6)
3×6 Matrix{Float64}:
 0.781215 0.864153 0.190954 0.513353 0.684802 0.304443
 0.717076 0.248486 0.394957 0.231284 0.572842 0.174061
 0.605211 0.814295 0.173915 0.0166475 0.597283 0.482926

julia> B = reinterpret(SVector{3,Float64}, vec(A))
6-element reinterpret(SVector{3, Float64}, ::Vector{Float64}):
 [0.7812150216015193, 0.7170764937769242, 0.6052113147390563]
 [0.8641531429329602, 0.24848604689820653, 0.8142954282406346]
 [0.1909536286428969, 0.3949565597637975, 0.17391456890697454]
 [0.5133528560285338, 0.23128368035325675, 0.016647532659139097]
 [0.6848016310407239, 0.5728415428873161, 0.5972827074627644]
 [0.3044430234630139, 0.17406109329315256, 0.4829260227587332]

julia> A[1,1] = 3.14159
3.14159

julia> B[1]
3-element SVector{3, Float64} with indices SOneTo(3):
 3.14159
 0.7170764937769242
 0.6052113147390563

```
