# Replace Vector at indices and return a copy

**URL:** <https://discourse.julialang.org/t/replace-vector-at-indices-and-return-a-copy/90692>\
**Category:** General Usage\
**Tags:** arrays, accessors\
**Created:** [November 23, 2022, 9:30am UTC](https://discourse.julialang.org/t/replace-vector-at-indices-and-return-a-copy/90692 "2022-11-23T09:30:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)\
**Post date:** [November 23, 2022, 9:30am UTC](https://discourse.julialang.org/t/replace-vector-at-indices-and-return-a-copy/90692/1 "2022-11-23T09:30:35Z")

</div>

What’s the easiest way to return a copy of a `Vector` with replaced elements at some indices with some specific values ?  
I can start with one approach:

```julia
julia> v = [10,20,30,40,50];

julia> inds = [2,5];

julia> vals = [200, 500];

julia> newv = let v2=deepcopy(v); v2[inds] = vals; v2; end
5-element Vector{Int64}:
  10
 200
  30
  40
 500

julia> v #still hasn't changed
5-element Vector{Int64}:
 10
 20
 30
 40
 50

```

I was expecting that there would be a `replaceat` and `replaceat!` function to do this.  
The implementation would be maybe something similar along the way:

```julia
function replaceat(v, inds, vals)
  v2 = deepcopy(v)
  v2[inds] = vals
  v2
end

function replaceat!(v, inds, vals)
  v[inds] = vals
  v
end

```

---

<div class="post-metadata">

**Author:** ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)\
**Post date:** [November 23, 2022, 9:43am UTC](https://discourse.julialang.org/t/replace-vector-at-indices-and-return-a-copy/90692/2 "2022-11-23T09:43:08Z")

</div>

```julia
v[inds].=vals

```

in place replacement

for a copy

```julia
v[inds]=vals

```

---

<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:** [November 23, 2022, 9:44am UTC](https://discourse.julialang.org/t/replace-vector-at-indices-and-return-a-copy/90692/3 "2022-11-23T09:44:14Z")

</div>

Why would you need a `replaceat` function, when the current solution is even simpler:

```julia
v[inds] = vals

```

?

Also, I would use `copy`.

---

<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:** [November 23, 2022, 9:45am UTC](https://discourse.julialang.org/t/replace-vector-at-indices-and-return-a-copy/90692/4 "2022-11-23T09:45:14Z")

</div>

No, those two are the same, both are in-place.

---

<div class="post-metadata">

**Author:** ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)\
**Post date:** [November 23, 2022, 9:48am UTC](https://discourse.julialang.org/t/replace-vector-at-indices-and-return-a-copy/90692/5 "2022-11-23T09:48:41Z")

</div>

I probably meant not allocating vs allocating 😳

---

<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:** [November 23, 2022, 9:51am UTC](https://discourse.julialang.org/t/replace-vector-at-indices-and-return-a-copy/90692/6 "2022-11-23T09:51:11Z")

</div>

They are actually indistinguishable. There’s no observable difference between them, it’s just that the dotted version involves broadcasting, but in the end it amounts to the same thing.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [November 23, 2022, 9:57am UTC](https://discourse.julialang.org/t/replace-vector-at-indices-and-return-a-copy/90692/7 "2022-11-23T09:57:45Z")

</div>

This is what Accessors.jl is for.

```julia
using Accessors

julia> x = [10,20,30]
3-element Vector{Int64}:
 10
 20
 30

julia> @set x[2] = 99
3-element Vector{Int64}:
 10
 99
 30

julia> x
3-element Vector{Int64}:
 10
 20
 30

```

---

<div class="post-metadata">

**Author:** ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)\
**Post date:** [November 23, 2022, 10:05am UTC](https://discourse.julialang.org/t/replace-vector-at-indices-and-return-a-copy/90692/8 "2022-11-23T10:05:09Z")

</div>

Both expressions are not what I need.  
Probably my bad.  
The original `v` vector must not be mutated.  
So first make a copy and then mutate the copy.  
Have a look at my working example.

Plus `v[inds]=vals` return `vals` which is bad for one-liners. I would like it to return the mutated copy of `v`.

I think the scenarios could be quite a few.  
I just have an original vector and I want get another one with just some replaced values based on index.  
There is `replace` and `replace!` but these work in the value level.

---

<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:** [November 23, 2022, 10:19am UTC](https://discourse.julialang.org/t/replace-vector-at-indices-and-return-a-copy/90692/9 "2022-11-23T10:19:03Z")

</div>

> [@filchristou](#):
>
> The original `v` vector must not be mutated.

Yes, this was clear. You were requesting both an in-place and an out-of-place version, so it was implied that you had to copy for the out-of-place version.

> [@filchristou](#):
>
> Plus `v[inds]=vals` return `vals` which is bad for one-liners. I would like it to return the mutated copy of `v`.

Then it seems like `Accessors.@set` is the way to go, or making your own.

---

<div class="post-metadata">

**Author:** ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)\
**Post date:** [November 23, 2022, 10:28am UTC](https://discourse.julialang.org/t/replace-vector-at-indices-and-return-a-copy/90692/10 "2022-11-23T10:28:18Z")

</div>

that’s it. Also works for multiple replacements with `@set v[inds] = vals` .  
perfect!
