# Create a 2D array from a 1D array with reference

**URL:** <https://discourse.julialang.org/t/create-a-2d-array-from-a-1d-array-with-reference/41758>\
**Category:** New to Julia\
**Tags:** array\
**Created:** [June 20, 2020, 12:07am UTC](https://discourse.julialang.org/t/create-a-2d-array-from-a-1d-array-with-reference/41758 "2020-06-20T00:07:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunjin](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@sunjin](https://discourse.julialang.org/u/sunjin)\
**Post date:** [June 20, 2020, 12:07am UTC](https://discourse.julialang.org/t/create-a-2d-array-from-a-1d-array-with-reference/41758/1 "2020-06-20T00:07:38Z")

</div>

Hi  
I am trying to use FiniteDiff package to compute a gradient of a function with an N-vector as input and a scalar as output. I need to write the gradient (an N-vector) to a pre-allocated vector (supplied by an optimization solver).

However, the FiniteDiff.finite\_difference\_jacobian! function will only write to a pre-allocated 1xN 2D array as the Jacobian matrix. How can I wrap the supplied 1D gradient vector by the optimization solver in a 1xN array to trick the finite\_difference\_jacobian! function into thinking it is getting a reference to a pre-allocated Jacobian matrix of the correct size, without new allocation and copying?

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [June 20, 2020, 1:28am UTC](https://discourse.julialang.org/t/create-a-2d-array-from-a-1d-array-with-reference/41758/2 "2020-06-20T01:28:58Z")

</div>

I believe what you are looking for is `reshape`:

[https://docs.julialang.org/en/v1/base/arrays/#Base.reshape](https://docs.julialang.org/en/v1/base/arrays/#Base.reshape)

---

<div class="post-metadata">

**Author:** ![sunjin](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@sunjin](https://discourse.julialang.org/u/sunjin)\
**Post date:** [June 20, 2020, 1:32am UTC](https://discourse.julialang.org/t/create-a-2d-array-from-a-1d-array-with-reference/41758/3 "2020-06-20T01:32:21Z")

</div>

reshape does not seem to operate in-place. It creates a new array. I’d like to write to the 1D vector pretending it is a 2D array.

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [June 20, 2020, 1:33am UTC](https://discourse.julialang.org/t/create-a-2d-array-from-a-1d-array-with-reference/41758/4 "2020-06-20T01:33:13Z")

</div>

> [@sunjin](#):
>
> reshape does not seem to operate in-place.

?!?!?! are we reading the same documentation?

> The two arrays share the same underlying data, so that the result is mutable if and only if `A` is mutable, and setting elements of one alters the values of the other.

---

<div class="post-metadata">

**Author:** ![sunjin](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@sunjin](https://discourse.julialang.org/u/sunjin)\
**Post date:** [June 20, 2020, 1:58am UTC](https://discourse.julialang.org/t/create-a-2d-array-from-a-1d-array-with-reference/41758/5 "2020-06-20T01:58:05Z")

</div>

You are right. My bad 🙂
