# How should I simplify nested loops?

**URL:** <https://discourse.julialang.org/t/how-should-i-simplify-nested-loops/53835>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [January 23, 2021, 4:52pm UTC](https://discourse.julialang.org/t/how-should-i-simplify-nested-loops/53835 "2021-01-23T16:52:14Z")\
**Posts on this page:** 15\
**Page:** 2

<div class="post-metadata">

**Author:** ![Jared\_shay](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jared\_shay](https://discourse.julialang.org/u/Jared_shay)\
**Post date:** [January 23, 2021, 8:22pm UTC](https://discourse.julialang.org/t/how-should-i-simplify-nested-loops/53835/24 "2021-01-23T20:22:10Z")

</div>

yes, I understand the `if-else` logic of julialang, but still get stuck with rewriting the below:

```julia
    Q = [
          ( (i <= n_prev && j <= n_prev) ? Q_prev[i, j] : ( i <= n_prev ? Q_aug[i, j-n_prev] :
          ( j <= n_prev ? Q_aug[j, i-n_prev] : Q_aug_diag[i-n_prev, j-n_prev] ) ) )::Float64
          for i=1:n_const, j=1:n_const]

```

could you show me the other way around? Thank you!

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [January 23, 2021, 8:30pm UTC](https://discourse.julialang.org/t/how-should-i-simplify-nested-loops/53835/25 "2021-01-23T20:30:45Z")

</div>

> [@Jared\_shay](#):
>
> yes, I understand the `if-else` logic of julialang, but still get stuck with rewriting the below:

Did you try our suggestions? How does it look when you do? Please post your attempt and we can suggest how to rework it if necessary.

I don’t intend to write it for you if the problem is simply that you don’t feel like doing it yourself…

---

<div class="post-metadata">

**Author:** ![Jared\_shay](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jared\_shay](https://discourse.julialang.org/u/Jared_shay)\
**Post date:** [January 23, 2021, 8:36pm UTC](https://discourse.julialang.org/t/how-should-i-simplify-nested-loops/53835/26 "2021-01-23T20:36:34Z")

</div>

yes, I wrote like this by taking above suggestion:

`Q` was like this:

```
Q = [
      ( (i <= n_prev && j <= n_prev) ? Q_prev[i, j] : ( i <= n_prev ? Q_aug[i, j-n_prev] :
      ( j <= n_prev ? Q_aug[j, i-n_prev] : Q_aug_diag[i-n_prev, j-n_prev] ) ) )::Float64
      for i=1:n_const, j=1:n_const]

```

I wrote like this as follow:

```julia
Q = []
    for i=1:n_const:
      for j=1:n_const:
        if i <= n_prev && j <= n_prev:
          if i <= n_prev:
            Q_prev[i,j] = Q_aug[i, j-n_prev]
          else j <= n_prev:
            Q_aug[j, i-n_prev] = Q_aug_diag[i-n_prev, j-n_prev]
          end
        end
      end
      push!(Q_aug_diag, Q_aug)
    end

```

could you correct me if I did it right here? I am not sure about this, and using `push!()`. Am I right about here? any thoughts? Thanks!

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [January 23, 2021, 8:39pm UTC](https://discourse.julialang.org/t/how-should-i-simplify-nested-loops/53835/27 "2021-01-23T20:39:22Z")

</div>

It should be like this instead:

```julia
Q = zeros(n_const,n_const)

for i = 1:n_const, j = 1:n_const
    if i <= n_prev && j <= n_prev 
        Q[i,j] = Q_prev[i, j]
    elseif i <= n_prev
        Q[i,j] = Q_aug[i, j-n_prev]
    elseif j <= n_prev
        Q[i,j] = Q_aug[j, i-n_prev]
    else 
        Q[i,j] = Q_aug_diag[i-n_prev, j-n_prev]
    end  
end 

```

---

<div class="post-metadata">

**Author:** ![Jared\_shay](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jared\_shay](https://discourse.julialang.org/u/Jared_shay)\
**Post date:** [January 23, 2021, 8:50pm UTC](https://discourse.julialang.org/t/how-should-i-simplify-nested-loops/53835/28 "2021-01-23T20:50:59Z")

</div>

thanks! this is what I want.

I am just curious about this: `for i = 1:n_const, j = 1:n_const` only works for `julialang` convention, or it also valid for python?

I mean, is this

```julia
        for i = 1:n_const, j = 1:n_const:
            ...
            ...

```

as same as this:

```julia
        for i = 1:n_const:
            for j = 1:n_const:
                ...
                ...

```

how these two different? any idea?

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [January 23, 2021, 9:02pm UTC](https://discourse.julialang.org/t/how-should-i-simplify-nested-loops/53835/30 "2021-01-23T21:02:08Z")

</div>

No, I don’t think you can write the same in Python (not an expert). You need the Pythonic way:

```julia
for i in range(1,n_const+1):
    for j in range(1,n_const+1):
        Q[i,j] = ...

```

---

<div class="post-metadata">

**Author:** ![Jared\_shay](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jared\_shay](https://discourse.julialang.org/u/Jared_shay)\
**Post date:** [January 23, 2021, 9:05pm UTC](https://discourse.julialang.org/t/how-should-i-simplify-nested-loops/53835/31 "2021-01-23T21:05:58Z")

</div>

Thanks a lot. Your comment was very helpful, I would accept it as solution.

Plus, I am curious about this:

is this code below:

```julia
    Q_aug = [( calc_dot((const_prev[i][1], const_prev[i][2], const_added[j][1], const_added[j][2]), K, y) )::Float64
          for i=1:n_prev, j=1:n_added]

```

equal to this one which I wrote below:

```julia
Q_aug= []
    for i=1:n_prev:
      for j=1:n_added:
        Q_aug[i,j]= calc_dot((const_prev[i][1], const_prev[i][2], const_added[j][1], const_added[j][2]), K, y)
      end
    end

```

function body of `calc_dot` is in [this gist](https://gist.github.com/adamFlyn/b27f42c2a0992a9c51fb04b66623cd2b). Am I right about on `Q_aug`? could you give me your possible feedback if I was wrong about it? Thanks again for your help in this community!

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [January 23, 2021, 9:08pm UTC](https://discourse.julialang.org/t/how-should-i-simplify-nested-loops/53835/32 "2021-01-23T21:08:54Z")

</div>

No, it is not equal. Your code block is neither valid Julia nor python. Look at @Seif_Shebl answer above for proper Julia code.

---

<div class="post-metadata">

**Author:** ![Jared\_shay](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jared\_shay](https://discourse.julialang.org/u/Jared_shay)\
**Post date:** [January 23, 2021, 9:10pm UTC](https://discourse.julialang.org/t/how-should-i-simplify-nested-loops/53835/33 "2021-01-23T21:10:21Z")

</div>

how that supposed to be corrected? should I use `psuh!()`? I am not quite sure about what I wrote. How could you write `Q_aug` in simplistic way? any idea?

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [January 23, 2021, 9:15pm UTC](https://discourse.julialang.org/t/how-should-i-simplify-nested-loops/53835/34 "2021-01-23T21:15:45Z")

</div>

Frankly, I think you should take a step back and try to work through the manual. IMO, you seem to be lacking basic understanding of Julia and its syntax (which of course is perfectly fine and expected for a beginner!). I don’t think it is too helpful to successively ask for rewrites of blocks of code here.

Also, I don’t fully understand why you would expect or want the same syntax to work in python and Julia. After all, these are different programming languages.

---

<div class="post-metadata">

**Author:** ![Jared\_shay](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jared\_shay](https://discourse.julialang.org/u/Jared_shay)\
**Post date:** [January 23, 2021, 9:38pm UTC](https://discourse.julialang.org/t/how-should-i-simplify-nested-loops/53835/35 "2021-01-23T21:38:48Z")

</div>

I looked into manual and followed above useful comments from @ [Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl) about the syntax. But I don’t understand why you think doing this way is wrong. Can you comment what was wrong about this?

```julia
Q_aug= []
    for i=1:n_prev:
      for j=1:n_added:
        Q_aug[i,j]= calc_dot((const_prev[i][1], const_prev[i][2], const_added[j][1], const_added[j][2]), K, y)
      end
    end

```

it is not quite straightforward to me at first place because there is no MWE that I could make. For julia learner, I looked into julialang documentation, and sample project that I am learning. Could you show what’s the valid way to write above? any idea?

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [January 23, 2021, 10:03pm UTC](https://discourse.julialang.org/t/how-should-i-simplify-nested-loops/53835/36 "2021-01-23T22:03:11Z")

</div>

As @carstenbauer said, you should go through the manual to get yourself familiar with the main syntax of Julia as you would do when learning C or MATLAB for the first time. You will find useful yet simple examples that will sharpen your skills in Julia. For example, the [Repeated Evaluation](https://docs.julialang.org/en/v1/manual/control-flow/#man-loops) section will teach you some ways to write and understand loops.

As for your code segment above, here are some comments:

1. The `Q_aug= []` will create a `Vector` with 0 elements of type `Any`, this will not be efficient in Julia because it is type-unstable
2. There is no `:`s at the end of `for`s or `if`s like in Python
3. Finally, julia is a different language from Python, so you should learn it and don’t expect Julia code to be identical to Python code. Julia is a newer language with cool modern features not available in Python or even in most similar-age languages, so why not put the effort to learn it and it will pay you off later?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 23, 2021, 10:41pm UTC](https://discourse.julialang.org/t/how-should-i-simplify-nested-loops/53835/37 "2021-01-23T22:41:13Z")

</div>

> [@Jared\_shay](#):
>
> But I don’t understand why you think doing this way is wrong

Did you test it? You will get an error message. To learn any programming language you have to code, test your code all the time, work through the errors, and have the manual and/or other references at your side.

---

<div class="post-metadata">

**Author:** ![Jared\_shay](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jared\_shay](https://discourse.julialang.org/u/Jared_shay)\
**Post date:** [January 23, 2021, 11:10pm UTC](https://discourse.julialang.org/t/how-should-i-simplify-nested-loops/53835/38 "2021-01-23T23:10:14Z")

</div>

could you direct me best sources for beginners like me? I jumped into Julia for project needs and looked into built-in functions and documentation, but I think not everything is crystal clear to newbie. I will take your comment series for learning. Thanks!

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 23, 2021, 11:19pm UTC](https://discourse.julialang.org/t/how-should-i-simplify-nested-loops/53835/39 "2021-01-23T23:19:35Z")

</div>

> **[Get started with Julia](https://julialang.org/learning/)**
>
> The official website for the Julia Language. Julia is a language that is fast, dynamic, easy to use, and open source. Click here to learn more.

[Previous page](https://discourse.julialang.org/t/how-should-i-simplify-nested-loops/53835.md?page=1)
