# What is the best way to update matrix in callable function?

**URL:** https://discourse.julialang.org/t/what-is-the-best-way-to-update-matrix-in-callable-function/84641
**Category:** General Usage
**Tags:** question
**Created:** [July 22, 2022, 4:20pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-update-matrix-in-callable-function/84641 "2022-07-22T16:20:27Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [July 22, 2022, 4:20pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-update-matrix-in-callable-function/84641/1 "2022-07-22T16:20:27Z")

</div>

I have a file called “run.jl” which has a function “update”. I have a main function which create matrix “Record”, which needs to be updated in a `for loop`. However, the matrix is not updated. What is the best way to update it with defining it as global variable?

```julia
#run.jl
function update(Record,iter)
Record += Record*iter;
end
# main code
include("run.jl");
select = true;
if select == true
  Record = [1,2;3,4];
end
iter=0;
for _ in 1:100
iter += 1;
update(Record,iter)
end

```

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [July 22, 2022, 5:03pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-update-matrix-in-callable-function/84641/2 "2022-07-22T17:03:30Z")

</div>

The matrix `Record` is not updated, because you are not updating it.

> [@Amro](#):
>
> `Record += Record*iter;`

does not update `Record`, but instead creates a new matrix, which has a value equal to `Record + Record*iter`.

If you want to update it you must do it in-place, which you can achieve with `.=` (notice the dot):

```julia
Record .+= Record .* iter

```

But you could also write this as

```julia
Record .*= (1 + iter)

```

---

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [July 22, 2022, 5:55pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-update-matrix-in-callable-function/84641/3 "2022-07-22T17:55:34Z")

</div>

I got it, thank you very much!  
Given that:

```julia
Record = [1,2;3,4];

```

which method below is faster (In terms of performance):

```julia
for _ in 1:100
Record .= Record*iter + Record*50 ; # Method-1
Record[:] = Record[:]*iter + Record[:]*50; # Method-2
end

```

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [July 22, 2022, 6:03pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-update-matrix-in-callable-function/84641/4 "2022-07-22T18:03:51Z")

</div>

Neither of them is good. The last line is definitely bad, but also in the first one you forget dots, and do unnecessary operations.

> [@Amro](#):
>
> `Record .= Record*iter + Record*50`

should rather be

```julia
Record .= Record .* iter .+ Record .* 50

```

Make sure all dots are there.

But it’s better to do

```julia
Record .*= (iter + 50) 

```

But you can replace the whole loop

> [@Amro](#):
>
> ```julia
> for _ in 1:100
> Record .= Record*iter + Record*50 ; # Method-1
> end
> 
> ```

with

```julia
Record .*= float(iter + 50)^100

```

The use of `float` is to avoid integer overflow.

---

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [July 22, 2022, 6:11pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-update-matrix-in-callable-function/84641/5 "2022-07-22T18:11:00Z")

</div>

Thank you!  
Actually, I tried to shorten the scheme. The original is:

```julia
Record = [1,2;3,4];
SS = [3,4;5,6];
iter = 0;
for _ in 1:100
iter += 1;
Record .= Record .* iter .+ SS .* 50 ; 
end

```

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [July 22, 2022, 6:13pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-update-matrix-in-callable-function/84641/6 "2022-07-22T18:13:29Z")

</div>

What is `iter`?

---

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [July 22, 2022, 6:15pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-update-matrix-in-callable-function/84641/7 "2022-07-22T18:15:44Z")

</div>

I’ve corrected.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [July 22, 2022, 7:18pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-update-matrix-in-callable-function/84641/8 "2022-07-22T19:18:03Z")

</div>

That’s a bit harder to simplify, at least for my vacation brain😎

But at least you can do

```julia
for iter in 1:100

```

instead of `iter += 1` inside the loop body.

---

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [July 22, 2022, 7:30pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-update-matrix-in-callable-function/84641/9 "2022-07-22T19:30:46Z")

</div>

Lol, thank you. I really appreciate your help during vacation.

---

<div class="post-metadata">

### Author: ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)
#### Post date: [July 23, 2022, 2:29am UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-update-matrix-in-callable-function/84641/10 "2022-07-23T02:29:16Z")

</div>

For a solution to a similar problem to this see the following comment; [How to build this sparsematrix in Julia - #9 by acxz](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/9)
