# How to add values to vector with repeated indices?

**URL:** <https://discourse.julialang.org/t/how-to-add-values-to-vector-with-repeated-indices/82422>\
**Category:** General Usage\
**Created:** [June 8, 2022, 9:52am UTC](https://discourse.julialang.org/t/how-to-add-values-to-vector-with-repeated-indices/82422 "2022-06-08T09:52:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Minimum-Pollution-96](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Minimum-Pollution-96](https://discourse.julialang.org/u/Minimum-Pollution-96)\
**Post date:** [June 8, 2022, 9:52am UTC](https://discourse.julialang.org/t/how-to-add-values-to-vector-with-repeated-indices/82422/1 "2022-06-08T09:52:52Z")

</div>

I’ll explain my need with an example.

```julia
a = [1:5;]
indices = [1,1,3,3];
values = [2,3,4,5];

```

I need to add `values` to the original vector `a` as per the `indices`.  
The solution for above example should be,

```julia
>>> [6,2,12,4,5]

```

I can do this with for loop. `a[indices] += values` doesn’t work (I get that). Is there any other way to do it?

---

<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 8, 2022, 10:02am UTC](https://discourse.julialang.org/t/how-to-add-values-to-vector-with-repeated-indices/82422/2 "2022-06-08T10:02:03Z")

</div>

This seems like a perfect use case for a for loop, I find it hard to imagine anything clearer. Why would you want to avoid a loop?

---

<div class="post-metadata">

**Author:** ![Minimum-Pollution-96](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Minimum-Pollution-96](https://discourse.julialang.org/u/Minimum-Pollution-96)\
**Post date:** [June 8, 2022, 10:12am UTC](https://discourse.julialang.org/t/how-to-add-values-to-vector-with-repeated-indices/82422/3 "2022-06-08T10:12:20Z")

</div>

Not necessary. Was just curious if there are any functions like accumarray (matlab) in julia!

---

<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 8, 2022, 10:26am UTC](https://discourse.julialang.org/t/how-to-add-values-to-vector-with-repeated-indices/82422/4 "2022-06-08T10:26:42Z")

</div>

JuliaHub suggests:

> **[GitHub - ChrisRackauckas/VectorizedRoutines.jl: Familiar vectorized routines...](https://github.com/ChrisRackauckas/VectorizedRoutines.jl)**
>
> Familiar vectorized routines from R/Python/MATLAB, plus some more. - GitHub - ChrisRackauckas/VectorizedRoutines.jl: Familiar vectorized routines from R/Python/MATLAB, plus some more.

But it looks pretty out of date.

---

<div class="post-metadata">

**Author:** ![Minimum-Pollution-96](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Minimum-Pollution-96](https://discourse.julialang.org/u/Minimum-Pollution-96)\
**Post date:** [June 8, 2022, 10:31am UTC](https://discourse.julialang.org/t/how-to-add-values-to-vector-with-repeated-indices/82422/5 "2022-06-08T10:31:42Z")

</div>

Thank you! Guess I’ll stick to the for loop. It is better as you said.
