# How do I reinterpret and reshape a multi-dimensional array \`J\` into a two-dimensional array \`j\` such that \`typeof(j) \<: Matrix\` is true in Julia 0.7

**URL:** <https://discourse.julialang.org/t/how-do-i-reinterpret-and-reshape-a-multi-dimensional-array-j-into-a-two-dimensional-array-j-such-that-typeof-j-matrix-is-true-in-julia-0-7/15911>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 5, 2018, 4:43am UTC](https://discourse.julialang.org/t/how-do-i-reinterpret-and-reshape-a-multi-dimensional-array-j-into-a-two-dimensional-array-j-such-that-typeof-j-matrix-is-true-in-julia-0-7/15911 "2018-10-05T04:43:09Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![zygmuntszpak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zygmuntszpak/32/2591_2.png) [@zygmuntszpak](https://discourse.julialang.org/u/zygmuntszpak)\
**Post date:** [October 5, 2018, 5:04am UTC](https://discourse.julialang.org/t/how-do-i-reinterpret-and-reshape-a-multi-dimensional-array-j-into-a-two-dimensional-array-j-such-that-typeof-j-matrix-is-true-in-julia-0-7/15911/2 "2018-10-05T05:04:03Z")

</div>

`Kristoffer Carlsson` suggested an _unsanctioned_ way of getting around this by using `unsafe_wrap`. As an example he showed:

```julia
julia> a = rand(5,5,5);

julia> unsafe_wrap(Vector{Float64}, pointer(a), (15,))
15-element Array{Float64,1}:
 0.39667876246883926
 0.22245479132465285

```

with the understanding that one would have to ensure that `a` is protected from the garbage collector.  
He also linked to a related discussion on reshaping `StaticArrays`.

[https://github.com/JuliaArrays/StaticArrays.jl/pull/496#issuecomment-421806480](https://github.com/JuliaArrays/StaticArrays.jl/pull/496#issuecomment-421806480)

`Moritz Schauer` suggested a clever alternative and safer approach which he attributed to `Keno Fisher` and dubbed _Keno’s obvious trick_:

Fill the reshaped _view_ of a full matrix of the shape you need, instead of reshaping the full matrix with a view into the shape you need.

This means that I start by declaring a Jacobian matrix, e.g. `J = zeros(4*10,12)` and then create a reshaped view of the Jacobian matrix: `Jv = reshape(reinterpret(Float64,J), 4, 10, 12)`.

I can then conveniently index into `Jv`. Since `Jv` is a view, I can pass `J` into `LsqFit.jl` because it contains the same data as `Jv` and is also a `Matrix`.

---

_[View the full topic](https://discourse.julialang.org/t/how-do-i-reinterpret-and-reshape-a-multi-dimensional-array-j-into-a-two-dimensional-array-j-such-that-typeof-j-matrix-is-true-in-julia-0-7/15911)._
