# Best practice for element-wise vector value update without memory allocation

**URL:** <https://discourse.julialang.org/t/best-practice-for-element-wise-vector-value-update-without-memory-allocation/94864>\
**Category:** Numerics\
**Tags:** question\
**Created:** [February 19, 2023, 3:52am UTC](https://discourse.julialang.org/t/best-practice-for-element-wise-vector-value-update-without-memory-allocation/94864 "2023-02-19T03:52:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charles](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@Charles](https://discourse.julialang.org/u/Charles)\
**Post date:** [February 19, 2023, 3:52am UTC](https://discourse.julialang.org/t/best-practice-for-element-wise-vector-value-update-without-memory-allocation/94864/1 "2023-02-19T03:52:07Z")

</div>

I am implementing Gaussian Elimination using Julia. The important thing is, when doing the elimination the row vectors are kept updating row by row. While writing a for loop to iterate over the row vector and assigning new value to it one by one does not allocate memory, I wonder whether there are any better solutions (but not package functions) that can do the vector assignment (like I need to update the old vector with an older vector and some basic calculation) without memory allocation?

A, B, C is a matrix below, for example I want to do:  
`A[1] = C[2] + B[2]`  
Any other efficient ways?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 19, 2023, 3:55am UTC](https://discourse.julialang.org/t/best-practice-for-element-wise-vector-value-update-without-memory-allocation/94864/2 "2023-02-19T03:55:03Z")

</div>

need example code to better help you

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 19, 2023, 4:00am UTC](https://discourse.julialang.org/t/best-practice-for-element-wise-vector-value-update-without-memory-allocation/94864/3 "2023-02-19T04:00:56Z")

</div>

First of all, you will get much better results if you do gausian elimination by column since Julia is column major. Secondly you can use broadcasting to do row/column updates without allocations. (you’ll also want to look at `@views`)

---

<div class="post-metadata">

**Author:** ![mofeing](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mofeing/32/47619_2.png) [@mofeing](https://discourse.julialang.org/u/mofeing)\
**Post date:** [March 12, 2023, 10:14am UTC](https://discourse.julialang.org/t/best-practice-for-element-wise-vector-value-update-without-memory-allocation/94864/4 "2023-03-12T10:14:12Z")

</div>

> [@Charles](#):
>
> I wonder whether there are any better solutions (but not package functions) that can do the vector assignment (like I need to update the old vector with an older vector and some basic calculation) without memory allocation?
> 
> A, B, C is a matrix below, for example I want to do:  
> `A[1] = C[2] + B[2]`  
> Any other efficient ways?

About this part, I usually code this as

```julia
A .= C .+ B

```

which is equivalent to the `broadcast!` function. The only problem is that you cannot shift the index (`A[1]` and `B[2],C[2]` in your example) but it can easily be solved using **views**. For example,

```julia
vA = @view A[1:end-1]
vB = @view B[2:end]
vC = @view C[2:end]

vA .= vC .+ vB

```
