# Increment some components of a vector

**URL:** <https://discourse.julialang.org/t/increment-some-components-of-a-vector/53769>\
**Category:** New to Julia\
**Created:** [January 22, 2021, 8:45am UTC](https://discourse.julialang.org/t/increment-some-components-of-a-vector/53769 "2021-01-22T08:45:11Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 22, 2021, 2:01pm UTC](https://discourse.julialang.org/t/increment-some-components-of-a-vector/53769/5 "2021-01-22T14:01:12Z")

</div>

> [@Benoit\_Gaudeul](#):
>
> I could obviously write a function with a loop to get the desired issue, but I’m afraid that will make the code much slower and slightly less clear.

Loops aren’t slow in Julia!

It sounds like you may be translating an existing “vectorized” code from some other language like Matlab or Python? You may not get much benefit from Julia unless you write in a completely different style.

For finite-volume/finite-difference algorithms, my feeling is that a simple loop is typically the clearest as well as the fastest method — just loop over your grid, and apply your stencil each point, e.g. `u[i,j] += c[i,j] * (u[i+1,j] + u[i-1,j] + u[i,j+1] + u[i,j-1] - 4u[i,j])` for a diffusion-like equation. Use [ghost cells](https://discourse.julialang.org/t/arrays-with-periodic-boundaries/4015/4) for boundary conditions.

---

_[View the full topic](https://discourse.julialang.org/t/increment-some-components-of-a-vector/53769)._
