# Optimization of checking on conditions in a 3d array

**URL:** <https://discourse.julialang.org/t/optimization-of-checking-on-conditions-in-a-3d-array/86282>\
**Category:** General Usage\
**Created:** [August 24, 2022, 6:08pm UTC](https://discourse.julialang.org/t/optimization-of-checking-on-conditions-in-a-3d-array/86282 "2022-08-24T18:08:02Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [August 24, 2022, 6:08pm UTC](https://discourse.julialang.org/t/optimization-of-checking-on-conditions-in-a-3d-array/86282/1 "2022-08-24T18:08:02Z")

</div>

I have an example of a python code snippet. I need to check all the numbers from the map\_spectrum array for 5 conditions, then put the new value in another array according to them. How can this be done in a more optimized version than in python code ?

```julia
x = y = np.arange(0.0, 1.0, 0.1)
map_spectrum = np.ones( (len(x), len(y), 6) )
map_color = np.ones( ( len(x), len(y) ) )

for ix, xv in enumerate(x):
    for iy, yv in enumerate(y):
            if map_spectrum[ix, iy, 0] > 0 and map_spectrum[ix, iy, 1] > 0 and map_spectrum[ix, iy, 2] == 0 and map_spectrum[ix, iy, 3:] < 0 :
                        map_color[ix, iy] = 1

```

In the real case, this array has a size of 101x101x6, that is, conditionally there are 10201 cells containing 6 values.  
All the cases of conditions in a short form:

First case +, +, 0, -, -, -  
Second case +, 0, -, - ,- ,-  
Third case 0, 0, -, -, -, -  
Four case 0, -, -, -  
Another

In addition, how to take into account machine zero when checking conditions.  
Example real values from array  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/6/c6861842c49e3ca3b0c024c03e8cbea38897a2a7.jpeg)

After creating a new array, it will be used as colors in heatmap that is

```julia
heatmap(x, y, map_color)

```

I tried to cycle through all the values and check them for 5 conditions, but it takes quite a long time

---

<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:** [August 24, 2022, 6:15pm UTC](https://discourse.julialang.org/t/optimization-of-checking-on-conditions-in-a-3d-array/86282/2 "2022-08-24T18:15:45Z")

</div>

> [@Sergey\_Novak](#):
>
> How can this be done in a more optimized version than in python code ?

this loop will run pretty fast in Julia, have you tried?

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [August 24, 2022, 6:20pm UTC](https://discourse.julialang.org/t/optimization-of-checking-on-conditions-in-a-3d-array/86282/3 "2022-08-24T18:20:22Z")

</div>

With a large array, this may take some time, also I would like to avoid using a loop or at least 5 conditions in a row in a loop. but even if we leave everything in this form, there remains the problem of how to check whether an infinitesimal value is really zero.

---

<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:** [August 24, 2022, 6:21pm UTC](https://discourse.julialang.org/t/optimization-of-checking-on-conditions-in-a-3d-array/86282/4 "2022-08-24T18:21:24Z")

</div>

> [@Sergey\_Novak](#):
>
> also I would like to avoid using a loop or at least 5 conditions in a row in a loop

how else do you want to do this?

> [@Sergey\_Novak](#):
>
> how to check whether an infinitesimal value is really zero.

use a cutoff

```julia
julia> isapprox(1e-15, 0.0; atol=1e-16)
false

julia> isapprox(1e-17, 0.0; atol=1e-16)
true

```

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [August 24, 2022, 6:28pm UTC](https://discourse.julialang.org/t/optimization-of-checking-on-conditions-in-a-3d-array/86282/5 "2022-08-24T18:28:07Z")

</div>

I’m thinking of using the equivalent switch statement from c++, but Julia doesn’t have it therefore, apparently there remains only the option of placing 5 conditions in the function.

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [August 24, 2022, 6:31pm UTC](https://discourse.julialang.org/t/optimization-of-checking-on-conditions-in-a-3d-array/86282/6 "2022-08-24T18:31:02Z")

</div>

The value can have, for example, 1 e4 or 1e-6 or 1.0762649529273773e-6 and this should be considered a machine zero  
I can only specify which precision is considered zero?

---

<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:** [August 24, 2022, 6:36pm UTC](https://discourse.julialang.org/t/optimization-of-checking-on-conditions-in-a-3d-array/86282/7 "2022-08-24T18:36:30Z")

</div>

> [@Sergey\_Novak](#):
>
> I can only specify which precision is considered zero?

`atol` is the absolute tolerance

---

<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:** [August 24, 2022, 6:38pm UTC](https://discourse.julialang.org/t/optimization-of-checking-on-conditions-in-a-3d-array/86282/8 "2022-08-24T18:38:47Z")

</div>

> [@Sergey\_Novak](#):
>
> I’m thinking of using the equivalent switch statement from c++,

you have five conditions `AND`ed together, how are you gonna use switch?

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 24, 2022, 7:42pm UTC](https://discourse.julialang.org/t/optimization-of-checking-on-conditions-in-a-3d-array/86282/9 "2022-08-24T19:42:12Z")

</div>

> [@Sergey\_Novak](#):
>
> I’m thinking of using the equivalent switch statement from c++, but Julia doesn’t have it therefore, apparently there remains only the option of placing 5 conditions in the function.

try using ternary operator in [this way](https://bkamins.github.io/julialang/2020/12/18/casewhen.html)

```julia
First case +, +, 0, -, -, -
Second case +, 0, -, - ,- ,-
Third case 0, 0, -, -, -, -
Four case 0, -, -, -
Another

ms0>0 && ms1>0 && ms2==0 && ms3<0 ? mp = "first" :
ms0>0 && ms1==0 ? mp = "second" :
ms0==0 && ms1==0 ? mp = "third" :
ms0==0 ? mp = "four" :
                                      mp = "another"

```

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [August 25, 2022, 7:56am UTC](https://discourse.julialang.org/t/optimization-of-checking-on-conditions-in-a-3d-array/86282/10 "2022-08-25T07:56:47Z")

</div>

Thank you, i read it and in general I understood how to use it except for one moment. I have not found an example in which the result would be written to a new variable and not the current one

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 25, 2022, 11:52am UTC](https://discourse.julialang.org/t/optimization-of-checking-on-conditions-in-a-3d-array/86282/11 "2022-08-25T11:52:50Z")

</div>

I’m not sure what your doubt is. If you are referring to the variable mp = 1, mp = 2 …, in its place you can put any expression with any variable

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [August 25, 2022, 12:06pm UTC](https://discourse.julialang.org/t/optimization-of-checking-on-conditions-in-a-3d-array/86282/12 "2022-08-25T12:06:17Z")

</div>

I understanded, sorry. I checked this with my code and it’s work.  
It remains to check `isapprox` on my data

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [August 25, 2022, 12:14pm UTC](https://discourse.julialang.org/t/optimization-of-checking-on-conditions-in-a-3d-array/86282/13 "2022-08-25T12:14:59Z")

</div>

`isaprox ` don’t work with number `1.9184694551117396e-5`

```julia
isapprox(1.9184694551117396e-5, 0.0; atol = 1e-8)

```

how to fix it or I don’t fully understand something

---

<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:** [August 25, 2022, 12:17pm UTC](https://discourse.julialang.org/t/optimization-of-checking-on-conditions-in-a-3d-array/86282/14 "2022-08-25T12:17:44Z")

</div>

your tolerance is smaller than the difference, make the tolerance larger
