# Most efficient way of adding elements within matrices in loops

**URL:** <https://discourse.julialang.org/t/most-efficient-way-of-adding-elements-within-matrices-in-loops/122789>\
**Category:** Performance\
**Tags:** question, matrices, loopvectorization\
**Created:** [November 18, 2024, 9:14pm UTC](https://discourse.julialang.org/t/most-efficient-way-of-adding-elements-within-matrices-in-loops/122789 "2024-11-18T21:14:55Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [November 18, 2024, 9:41pm UTC](https://discourse.julialang.org/t/most-efficient-way-of-adding-elements-within-matrices-in-loops/122789/2 "2024-11-18T21:41:09Z")

</div>

Welcome to this forum!

We can better engage with your question if you [follow the PSA](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757/1) on questions.

The thing that your example is missing is “self contained runnable + mimics your true usecase”. To give an example in style how this could look like:

```julia
using BenchmarkTools

I=10;J=11;K=12; N=500; A = rand(N); C=rand(1:N, I, J, K); B=rand(I,J,K);

function update_indices!(A, B, C)
  for i in 1:I, j in 1:J, k in 1:K
    vector_indices_to_update = C[i,j,k];
    A[vector_indices_to_update] += B[i,j,k]
  end
end

@btime update_indices!($A, $B, $C)

```

What you need to do in order to get useful help is write code snippets that initialize A,B,C. “the length of A is significantly larger…” is all fine and dandy, but it is better to simply incorporate that into the sample problem you show us.

Please also think about all the details, and make sure that they all mirror your true usecase. If your real data is not sorted, then you SHOULD NOT give sorted samples; if your real data is Float32 then you give us Float32, etc etc.

Don’t be go :surprised\_pikachu: if you post `collect(1:10_000)` as a test vector and proposed solutions delete the `collect`, a la

```julia
julia> @btime sum($(1:1000_000))
  2.016 ns (0 allocations: 0 bytes)

```

---

_[View the full topic](https://discourse.julialang.org/t/most-efficient-way-of-adding-elements-within-matrices-in-loops/122789)._
