# If-else loop failure for a large array

**URL:** <https://discourse.julialang.org/t/if-else-loop-failure-for-a-large-array/97180>\
**Category:** General Usage\
**Tags:** question, differentialequation, for-loop\
**Created:** [April 6, 2023, 1:51pm UTC](https://discourse.julialang.org/t/if-else-loop-failure-for-a-large-array/97180 "2023-04-06T13:51:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sh\_166](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sh_166/32/43965_2.png) [@Sh\_166](https://discourse.julialang.org/u/Sh_166)\
**Post date:** [April 6, 2023, 1:51pm UTC](https://discourse.julialang.org/t/if-else-loop-failure-for-a-large-array/97180/1 "2023-04-06T13:51:28Z")

</div>

I have the following code:

```plaintext
    if sol[5,i] - sol_1[5,i] >= 0.0
        s[i] = sol[5,i] - sol_1[5,i]
    else
        s[i] = sol_1[5,i] - sol[5,i]
    end
end

```

Now, here, sol and sol\_1 correspond to 2 solutions I have from DifferentialEquations.jl (basically, I would like to plot the absolute value of the difference at each timestep; my solution vectors are same sized and have 6 variables - here, I am only considering the 5th variable).

Now, problem with this is that the code doesn’t work. I get both positive and negative values for s which certainly not what I intended. In fact, I ran the following line,

```julia
s = sol[5,i] - sol_1[5,i]

```

This and the above code give me the same s. Any help would be appreciated!

---

<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:** [April 6, 2023, 2:00pm UTC](https://discourse.julialang.org/t/if-else-loop-failure-for-a-large-array/97180/2 "2023-04-06T14:00:24Z")

</div>

See [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) — it’s hard to help you without a minimal code that we can actually run to reproduce your problem.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [April 6, 2023, 2:33pm UTC](https://discourse.julialang.org/t/if-else-loop-failure-for-a-large-array/97180/3 "2023-04-06T14:33:29Z")

</div>

> [@Sh\_166](#):
>
> the absolute value of the difference

```julia
s = abs(sol[5,i] - sol_1[5,i])

```

You haven’t provided enough information for anyone to tell why your original code doesn’t work (suggested in the link in the other response). Maybe there’s something else in your loop overwriting `s[i]`. Sometimes unexpected branching can be due to comparing with NaN, but if that were the case you’d likely be complaining about `s` getting filled with NaN.

---

<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 6, 2023, 10:18pm UTC](https://discourse.julialang.org/t/if-else-loop-failure-for-a-large-array/97180/4 "2023-04-06T22:18:46Z")

</div>

Can you explain why you are using `if`/`else` instead of `abs`? It seems like a very particular, but odd, choice.

---

<div class="post-metadata">

**Author:** ![Sh\_166](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sh_166/32/43965_2.png) [@Sh\_166](https://discourse.julialang.org/u/Sh_166)\
**Post date:** [April 7, 2023, 8:21am UTC](https://discourse.julialang.org/t/if-else-loop-failure-for-a-large-array/97180/5 "2023-04-07T08:21:03Z")

</div>

Ah, sorry. My code is quite large, so I skipped it (it takes time to run, and contains a lot of irrelevant portions). However, I did manage to figure it out (i.e. I managed to compute what I wanted), so there’s that.

---

<div class="post-metadata">

**Author:** ![Sh\_166](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sh_166/32/43965_2.png) [@Sh\_166](https://discourse.julialang.org/u/Sh_166)\
**Post date:** [April 7, 2023, 8:22am UTC](https://discourse.julialang.org/t/if-else-loop-failure-for-a-large-array/97180/6 "2023-04-07T08:22:24Z")

</div>

Actually, I did at first. Must’ve made a typo there because it kept showing me error, so I went a more naive way. Anyhow, using abs.() works fine now, so I have just skipped the loop. Thanks a lot.
