# Can I create a reference to a section of a matrix without copying its elements?

**URL:** https://discourse.julialang.org/t/can-i-create-a-reference-to-a-section-of-a-matrix-without-copying-its-elements/106813
**Category:** New to Julia
**Tags:** arrays, views
**Created:** [November 27, 2023, 4:28pm UTC](https://discourse.julialang.org/t/can-i-create-a-reference-to-a-section-of-a-matrix-without-copying-its-elements/106813 "2023-11-27T16:28:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Bottom](https://avatars.discourse-cdn.com/v4/letter/b/b3f665/32.png) [@Bottom](https://discourse.julialang.org/u/Bottom)
#### Post date: [November 27, 2023, 4:28pm UTC](https://discourse.julialang.org/t/can-i-create-a-reference-to-a-section-of-a-matrix-without-copying-its-elements/106813/1 "2023-11-27T16:28:39Z")

</div>

In my code I have some 3D matrices M[m,i,j] where I iterate much less frequent over the first index (m: timestep) than over the other two (i,j: spatial discretization). I would like to have an alias for M[m, :, :] so I can operate on a 2D matrix within the timestep loop to make the code more compact and easy to read, especially when passing the 2D matrix to multiple other functions.

In the first block, when I change X, M changes too. I would like to achieve the same behavior in the second block where M is not changed (and I assume its values are copied to X because I observed increased runtime).

```julia
M = zeros(3,3,3)
X = M
X[1,1,1] += 1
@show M

M = zeros(3,3,3)
X = M[1,:,:]
X[1,1] += 1
@show M

```

Is there a way to create such a reference or is my need for it an indicator that my design is fundamentally flawed?  
Thank you very much in advance!

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [November 27, 2023, 4:37pm UTC](https://discourse.julialang.org/t/can-i-create-a-reference-to-a-section-of-a-matrix-without-copying-its-elements/106813/2 "2023-11-27T16:37:45Z")

</div>

Hello and welcome to the community 👋

You want to create a _view_ into the array. You can do this with the function `view` or the macros `@view` or `@views`.

```julia
X = @views M[1,:,:]
X[1,1] += 1
@show M

```

---

<div class="post-metadata">

### Author: ![Bottom](https://avatars.discourse-cdn.com/v4/letter/b/b3f665/32.png) [@Bottom](https://discourse.julialang.org/u/Bottom)
#### Post date: [November 27, 2023, 4:48pm UTC](https://discourse.julialang.org/t/can-i-create-a-reference-to-a-section-of-a-matrix-without-copying-its-elements/106813/3 "2023-11-27T16:48:20Z")

</div>

That’s very elegant and makes so much sense. I wasn’t familiar with views before. Thank you!

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [November 27, 2023, 8:01pm UTC](https://discourse.julialang.org/t/can-i-create-a-reference-to-a-section-of-a-matrix-without-copying-its-elements/106813/4 "2023-11-27T20:01:24Z")

</div>

I’ll add a little bit of additional commentary:

As a column-major object, the “fastest” indices in an `Array` are the first ones. This matches MATLAB but is opposite to NumPy. This is to say that you might get faster performance storing `M` as `M[i,j,m]` rather than your current `M[m,i,j]`, since it appears that your “tight loop” actions are over `i,j` rather than `m`.

Further (and unlike MATLAB, NumPy, and some other tools), Julia handles arrays-of-arrays quite nicely and with decent performance. If you’re commonly slicing `M[:,i,j]` _and also_ `M[m,:,:]` then you might truly be better off with your current 3D array approach. On the other hand, if most of your slicing is only one of those, then arrays-of-arrays are probably cleaner to work with. From your description, you would probably organize things to index as `M[m][i,j]` if you went this direction.
