# Matrix indexing and manipulation

**URL:** <https://discourse.julialang.org/t/matrix-indexing-and-manipulation/82563>\
**Category:** General Usage\
**Created:** [June 10, 2022, 12:57pm UTC](https://discourse.julialang.org/t/matrix-indexing-and-manipulation/82563 "2022-06-10T12:57:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emilio](https://avatars.discourse-cdn.com/v4/letter/e/f475e1/32.png) [@Emilio](https://discourse.julialang.org/u/Emilio)\
**Post date:** [June 10, 2022, 12:57pm UTC](https://discourse.julialang.org/t/matrix-indexing-and-manipulation/82563/1 "2022-06-10T12:57:41Z")

</div>

Hello there! I’m quite new to Julia and facing following issue: assume that we have a matrix:

mat = [1 2 3 4; 2 3 4 5]

and I want to manipulate a single value from a part of the matrix like this:

mat[1:3,1:3][1] = 2

then Julia would create a view and not modify the value in my original matrix mat. Up until now I worked around like this:

temporaryMat = mat[1:3,1:3] =\> temporaryMat[1] = 2 =\> mat[1:3,1:3] = temporaryMat.

I am well aware, that in this case I could have just manipulated the first value of our original mat. However I am currently working with a three-dimensional Matrix and I want to manipulate individual values.  
I find this Solution to be rather inelegant.  
So my question:  
is there a way to achieve this with only one command?  
I am really looking forward to an answer and thank you all in advance.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 10, 2022, 1:12pm UTC](https://discourse.julialang.org/t/matrix-indexing-and-manipulation/82563/2 "2022-06-10T13:12:00Z")

</div>

When you say “Julia would create a view” you’ve got it the wrong way around - it’s precisely the fact that Julia **doesn’t** create a view but a copy when you do `mat[1:3, 1:3]` that what you’re trying fails.

You can get around this by explicitly creating a view:

```julia
julia> @view(mat[1:2, 1:2])[1] = 5
5

julia> mat
2×4 Matrix{Int64}:
 5 2 3 4
 2 3 4 5

```

---

<div class="post-metadata">

**Author:** ![Emilio](https://avatars.discourse-cdn.com/v4/letter/e/f475e1/32.png) [@Emilio](https://discourse.julialang.org/u/Emilio)\
**Post date:** [June 10, 2022, 1:15pm UTC](https://discourse.julialang.org/t/matrix-indexing-and-manipulation/82563/3 "2022-06-10T13:15:11Z")

</div>

Thank you so much for the quick answer, it helps me a lot.  
Have a nice day, sir!
