# Changing the values of a subvector returned by a function doesn't change the original vector.

**URL:** https://discourse.julialang.org/t/changing-the-values-of-a-subvector-returned-by-a-function-doesnt-change-the-original-vector/22746
**Category:** General Usage
**Created:** [April 4, 2019, 12:24pm UTC](https://discourse.julialang.org/t/changing-the-values-of-a-subvector-returned-by-a-function-doesnt-change-the-original-vector/22746 "2019-04-04T12:24:07Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![matteoneri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matteoneri/32/7848_2.png) [@matteoneri](https://discourse.julialang.org/u/matteoneri)
#### Post date: [April 4, 2019, 12:24pm UTC](https://discourse.julialang.org/t/changing-the-values-of-a-subvector-returned-by-a-function-doesnt-change-the-original-vector/22746/1 "2019-04-04T12:24:07Z")

</div>

If I modify a subselection af an array, the array itself is modified (I assume the subselection is a reference to the same values. This behaviour is expected.

```julia
1×4 Array{Int64,2}:
 1 2 3 4

julia> v[[1 2]] = [5 5]
1×2 Array{Int64,2}:
 5 5

julia> v
1×4 Array{Int64,2}:
 5 5 3 4

```

If I have a function that has as an argument a vector `a` , modifies a subvector of `a` and returns a subvector `b` of `a` , something unexpected happens:

- the values of `a` are changed as expected
- changing the values of `b` doesn’t change `a` anymore [not expected].  
Is it wanted?

```julia
1×4 Array{Int64,2}:
 1 2 3 4

julia> function f(a)
           a[[1 2]] = [6 6]
           a[[1 2]]
       end
f (generic function with 1 method)

julia> vv = f(v)
1×2 Array{Int64,2}:
 6 6

julia> v
1×4 Array{Int64,2}:
 6 6 3 4

julia> vv[1] = 1
1

julia> vv
1×2 Array{Int64,2}:
 1 6

julia> v
1×4 Array{Int64,2}:
 6 6 3 4

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 4, 2019, 12:32pm UTC](https://discourse.julialang.org/t/changing-the-values-of-a-subvector-returned-by-a-function-doesnt-change-the-original-vector/22746/2 "2019-04-04T12:32:19Z")

</div>

`[]` (`getindex`) creates a new array. Use `SubArray`s ([`view`](https://docs.julialang.org/en/v1/base/arrays/#Views-(SubArrays-and-other-view-types)-1)).

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 4, 2019, 1:02pm UTC](https://discourse.julialang.org/t/changing-the-values-of-a-subvector-returned-by-a-function-doesnt-change-the-original-vector/22746/3 "2019-04-04T13:02:47Z")

</div>

> [@matteoneri](#):
>
> ```julia
> a[[1 2]] = [6 6]
> a[[1 2]]
> 
> ```

These two lines look like they do at least partly the same thing, but they don’t. The _second_ line is syntactic sugar for `getindex`, which creates a copy of the data at those indices.

The _first_ line looks like it does `getindex` and then assigns something to the output of that, but that’s not the case. What you are seeing is sugar for `setindex!(a, ...)` which mutates the `a` at certain indices.

It would perhaps have been nice if the syntaxes were a bit more distinct, but I have no suggestion for how that could be done.

---

<div class="post-metadata">

### Author: ![matteoneri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matteoneri/32/7848_2.png) [@matteoneri](https://discourse.julialang.org/u/matteoneri)
#### Post date: [April 5, 2019, 12:59pm UTC](https://discourse.julialang.org/t/changing-the-values-of-a-subvector-returned-by-a-function-doesnt-change-the-original-vector/22746/4 "2019-04-05T12:59:18Z")

</div>

Thanks! Problem solved.  
`view` was exactly what I needed.  
I got confused cause I thought the behaviour was similar to numpy arrays, while it is only for certain features.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [April 5, 2019, 1:59pm UTC](https://discourse.julialang.org/t/changing-the-values-of-a-subvector-returned-by-a-function-doesnt-change-the-original-vector/22746/5 "2019-04-05T13:59:25Z")

</div>

👍 NumPy array slices are views, whereas Julia’s are copies unless you use `@view`.

---

<div class="post-metadata">

### Author: ![Azamat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azamat/32/6892_2.png) [@Azamat](https://discourse.julialang.org/u/Azamat)
#### Post date: [April 7, 2019, 12:01pm UTC](https://discourse.julialang.org/t/changing-the-values-of-a-subvector-returned-by-a-function-doesnt-change-the-original-vector/22746/6 "2019-04-07T12:01:57Z")

</div>

Shouldn’t slices `[]` return views like in NumPy to avoid allocations? This seems like low-hanging optimization that I would expect to be implemented in Julia. Or am I missing something?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [April 7, 2019, 12:19pm UTC](https://discourse.julialang.org/t/changing-the-values-of-a-subvector-returned-by-a-function-doesnt-change-the-original-vector/22746/7 "2019-04-07T12:19:23Z")

</div>

> [@Azamat](#):
>
> Or am I missing something?

Yes, firstly, it is a breaking change and secondly, it is entirely up to what you do with the slice later that determines if copying or not improves performance. It was decided to copy by default since that is the safer alternative (reduce aliasing) and in case the time copying turns out to be significant you can opt in to aliasing (`view`).

---

<div class="post-metadata">

### Author: ![Azamat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azamat/32/6892_2.png) [@Azamat](https://discourse.julialang.org/u/Azamat)
#### Post date: [April 7, 2019, 3:00pm UTC](https://discourse.julialang.org/t/changing-the-values-of-a-subvector-returned-by-a-function-doesnt-change-the-original-vector/22746/8 "2019-04-07T15:00:56Z")

</div>

Most of the time when I am using `[]` copying is unnecessary. The fact that NumPy chose to return views by default, suggests that my experience is shared by many. So… I’m inclined to think that if we do some empirical studies on the written julia code then we will conclude that views are better choice.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 7, 2019, 4:07pm UTC](https://discourse.julialang.org/t/changing-the-values-of-a-subvector-returned-by-a-function-doesnt-change-the-original-vector/22746/9 "2019-04-07T16:07:31Z")

</div>

Going from memory here (since I’m bad ad searching github):

Back a few years ago, views were quite expensive, so there was a clear performance benefit to returning copies. But the way I understood it, the plan was to switch to slices as views once the performance was sorted out, and this was generally expected by most.

Gradually there was a change in opinion, and once views became fast, doubts crept in that switching over was a good idea after all. It would be _massively_ breaking, and the performance benefits were mixed, and absolutely not conclusive.

The question whether to stay with copies or switch to views was very thoroughly discussed, and finally a decision was reached to stick with copies. If you search for keywords like ‘views’, ‘arraymageddon’, ‘arraypocalypse’, and a few other, you can probably find a lot of discussion about this.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 7, 2019, 5:40pm UTC](https://discourse.julialang.org/t/changing-the-values-of-a-subvector-returned-by-a-function-doesnt-change-the-original-vector/22746/10 "2019-04-07T17:40:52Z")

</div>

> [@Azamat](#):
>
> I’m inclined to think that if we do some empirical studies on the written julia code then we will conclude that views are better choice.

This depends mostly on the objects, the implementation of views, and access patterns. As explained in the FAQ, [copying data is not always bad](https://docs.julialang.org/en/v1/manual/performance-tips/#Copying-data-is-not-always-bad-1).

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [April 7, 2019, 5:57pm UTC](https://discourse.julialang.org/t/changing-the-values-of-a-subvector-returned-by-a-function-doesnt-change-the-original-vector/22746/11 "2019-04-07T17:57:45Z")

</div>

> [@Azamat](#):
>
> I’m inclined to think that if we do some empirical studies on the written julia code then we will conclude that views are better choice.

Just to be clear, this (extensive) discussion has already been had and the decision made. It will not be changed.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 7, 2019, 9:41pm UTC](https://discourse.julialang.org/t/changing-the-values-of-a-subvector-returned-by-a-function-doesnt-change-the-original-vector/22746/12 "2019-04-07T21:41:25Z")

</div>

Here is an issue discussion you could read about this question: [https://github.com/JuliaLang/julia/issues/3701](https://github.com/JuliaLang/julia/issues/3701)
