# Multiplying Matrix inside a loop

**URL:** https://discourse.julialang.org/t/multiplying-matrix-inside-a-loop/113352
**Category:** General Usage
**Tags:** question
**Created:** [April 22, 2024, 5:47pm UTC](https://discourse.julialang.org/t/multiplying-matrix-inside-a-loop/113352 "2024-04-22T17:47:40Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Digidi](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Digidi](https://discourse.julialang.org/u/Digidi)
#### Post date: [April 22, 2024, 5:47pm UTC](https://discourse.julialang.org/t/multiplying-matrix-inside-a-loop/113352/1 "2024-04-22T17:47:40Z")

</div>

Hello, i try to write an alorithm where the Matrix is iterated ofer a table value. In each step the Matrix should be multiplyed with the Matrix before. I tried severl approches but i didn’t find something which is working. Anybody have an idea, how i can do it?

So at the End i want

```julia
M_final = M_display_1* M_display_2 * M_display_3

```

My code now working:

```julia
using LinearAlgebra
using SymbolicUtils
using Plots

@syms a_n b_n c_n d_n
@syms a b c d
@syms a1 b1 c1 d1
@syms a2 b2 c2 d2
@syms a3 b3 c3 d3

values_table = [
  (a1, b1, c1, d1),
  (a2, b2, c2, d2),
  (a3, b3, c3, d3),
]

M_n( a_n, b_n, c_n, d_n ) = [ a_n 0 0 0; 
                                0 b_n 0 0;
                                0 0 c_n 0;
                                0 0 0 d_n ]

M_final = Matrix(I, 4, 4)

for (i, row) in enumerate(values_table )
 a, b, c, d = row 

    M_display= M_n(a, b, c, d )

   global M_final *= M_display
 
end

```

---

<div class="post-metadata">

### Author: ![aryavorskiy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aryavorskiy/32/43466_2.png) [@aryavorskiy](https://discourse.julialang.org/u/aryavorskiy)
#### Post date: [April 22, 2024, 6:46pm UTC](https://discourse.julialang.org/t/multiplying-matrix-inside-a-loop/113352/2 "2024-04-22T18:46:10Z")

</div>

Hi!

You forgot to define the `M_final` variable. Try adding this before the loop

```julia
M_final = Matrix(I, 4, 4)

```

Does it solve your issue?

---

<div class="post-metadata">

### Author: ![Digidi](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Digidi](https://discourse.julialang.org/u/Digidi)
#### Post date: [April 22, 2024, 7:31pm UTC](https://discourse.julialang.org/t/multiplying-matrix-inside-a-loop/113352/3 "2024-04-22T19:31:23Z")

</div>

Hello, thank you for your answer. I did this before. But i get always this message:

```julia
 Exception has occurred: UndefVarError

UndefVarError: `M_final` not defined

```

When i define it inside the loop than it is working but result is wrong.

---

<div class="post-metadata">

### Author: ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)
#### Post date: [April 22, 2024, 9:02pm UTC](https://discourse.julialang.org/t/multiplying-matrix-inside-a-loop/113352/4 "2024-04-22T21:02:15Z")

</div>

Hi there, try defining `M_final` outside the loop and using it in the loop with the `global` keyword ([Control Flow · The Julia Language](https://docs.julialang.org/en/v1/manual/control-flow/#man-loops)). Alternatively the loop with the definition of `M_final` could be put in a function.

---

<div class="post-metadata">

### Author: ![Digidi](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Digidi](https://discourse.julialang.org/u/Digidi)
#### Post date: [April 22, 2024, 9:09pm UTC](https://discourse.julialang.org/t/multiplying-matrix-inside-a-loop/113352/5 "2024-04-22T21:09:24Z")

</div>

Thank you very much. Global solved it for me.

---

<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: [April 22, 2024, 10:40pm UTC](https://discourse.julialang.org/t/multiplying-matrix-inside-a-loop/113352/6 "2024-04-22T22:40:31Z")

</div>

> [@Digidi](#):
>
> Global solved it for me.

I would not recommend `global`. Put the code in a function instead.

---

<div class="post-metadata">

### Author: ![Digidi](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Digidi](https://discourse.julialang.org/u/Digidi)
#### Post date: [April 23, 2024, 10:28am UTC](https://discourse.julialang.org/t/multiplying-matrix-inside-a-loop/113352/7 "2024-04-23T10:28:10Z")

</div>

Hello, can you please describe why i should do it in funcitons?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [April 23, 2024, 10:54am UTC](https://discourse.julialang.org/t/multiplying-matrix-inside-a-loop/113352/8 "2024-04-23T10:54:22Z")

</div>

Can’t speak for DNF of course but:

> **[Global Variables are Evil and Unsafe - ForrestTheWoods](https://www.forrestthewoods.com/blog/global-variables-are-evil-and-unsafe/)**
>
> Global variables are evil and unsafe. You should stop using them.

(or hundreds of other pieces all across the internet if you search for them)

---

<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: [April 23, 2024, 11:28am UTC](https://discourse.julialang.org/t/multiplying-matrix-inside-a-loop/113352/9 "2024-04-23T11:28:08Z")

</div>

Indeed, globals are _not good_, and for Julia they are particularly bad, as they also harm performance. The performance in Julia is built around type stability and compiling functions. Avoiding working with globals in global scope is the number one performance tip in the manual: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/)

---

<div class="post-metadata">

### Author: ![Digidi](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Digidi](https://discourse.julialang.org/u/Digidi)
#### Post date: [April 23, 2024, 9:51pm UTC](https://discourse.julialang.org/t/multiplying-matrix-inside-a-loop/113352/10 "2024-04-23T21:51:37Z")

</div>

Thank you for this helpful informations. But i run in an issue that i have no idea how to do it in function. at the end i have to call the function again and have to iterate over the function to get my result.
