This became an issue in a portion of my code where I wanted to reshape a Jacobian matrix into a multi-dimensional array. The reshaping facilitated convenient indexing into the elements of the Jacobian matrix whilst I was looping over the data that is required to construct the entries of the matrix.
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.
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.