# Modifying vectors in for loop doesn't work

**URL:** https://discourse.julialang.org/t/modifying-vectors-in-for-loop-doesnt-work/72391
**Category:** New to Julia
**Tags:** question, vector
**Created:** [December 1, 2021, 5:42pm UTC](https://discourse.julialang.org/t/modifying-vectors-in-for-loop-doesnt-work/72391 "2021-12-01T17:42:42Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![dhryniuk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dhryniuk/32/28885_2.png) [@dhryniuk](https://discourse.julialang.org/u/dhryniuk)
#### Post date: [December 1, 2021, 5:42pm UTC](https://discourse.julialang.org/t/modifying-vectors-in-for-loop-doesnt-work/72391/1 "2021-12-01T17:42:42Z")

</div>

I would like to have a function which would generate vectors of the same length and average them together into a single vector. I can’t quite understand why the following example wouldn’t work (the value of vector remains the same as initially assigned in the second line):

```julia
function f(N)
     vector = rand(3)
     for n in 2:N
          v = rand(3)
          (vector).+v
          display(vector)
     end
     (vector)./N
     return vector
end

display(f(5))

```

---

<div class="post-metadata">

### Author: ![trahflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trahflow/32/30585_2.png) [@trahflow](https://discourse.julialang.org/u/trahflow)
#### Post date: [December 1, 2021, 5:47pm UTC](https://discourse.julialang.org/t/modifying-vectors-in-for-loop-doesnt-work/72391/2 "2021-12-01T17:47:30Z")

</div>

you do calculations on `vector` but don’t reassign it back.  
E.g. in line 5 you probably want

```julia
vector = vector .+ v

```

or equivalently

```julia
vector .+= v

```

Same goes for line 8
