# Condition on a whole array

**URL:** <https://discourse.julialang.org/t/condition-on-a-whole-array/9148>\
**Category:** General Usage\
**Created:** [February 18, 2018, 5:22pm UTC](https://discourse.julialang.org/t/condition-on-a-whole-array/9148 "2018-02-18T17:22:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ferran\_Mazzanti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ferran_mazzanti/32/7458_2.png) [@Ferran\_Mazzanti](https://discourse.julialang.org/u/Ferran_Mazzanti)\
**Post date:** [February 18, 2018, 5:22pm UTC](https://discourse.julialang.org/t/condition-on-a-whole-array/9148/1 "2018-02-18T17:22:43Z")

</div>

Hi,  
I have a fairly simple question that would like to sort out with your kind help 🙂 The fact is that I like quite a lot the conditional syntax  
{something} ? true : false  
which works great for short decissions like

```julia
x>0.5 ? 1 : 0

```

Is it possible to do something similar not for scalar valued x but for x a vector or array? How would that be done? I understand that

```julia
x.>0

```

works, but what then? Of course I can use the map functionto do what I want, but was wondering if it is possible to do something simpler or performance wise (?)

Best regards and thanks,

Ferran.

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [February 18, 2018, 5:33pm UTC](https://discourse.julialang.org/t/condition-on-a-whole-array/9148/2 "2018-02-18T17:33:32Z")

</div>

How about

```julia
[v > 0.5 ? 1 : 0 for v in x]

```

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [February 18, 2018, 5:54pm UTC](https://discourse.julialang.org/t/condition-on-a-whole-array/9148/3 "2018-02-18T17:54:38Z")

</div>

The generator code above performs really well but `ifelse` seems to be even better:

```julia
julia> x = rand(100000);

julia> bar(x) = [v > 0.5 ? 1 : 0 for v in x]
bar (generic function with 1 method)

julia> @btime bar($x);
  103.453 μs (3 allocations: 781.34 KiB)

julia> foo(x) = ifelse.(x .> 0.5, 1, 0)
foo (generic function with 1 method)

julia> @btime foo($x);
  91.008 μs (2 allocations: 781.33 KiB)

```

---

<div class="post-metadata">

**Author:** ![Ferran\_Mazzanti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ferran_mazzanti/32/7458_2.png) [@Ferran\_Mazzanti](https://discourse.julialang.org/u/Ferran_Mazzanti)\
**Post date:** [February 18, 2018, 6:21pm UTC](https://discourse.julialang.org/t/condition-on-a-whole-array/9148/4 "2018-02-18T18:21:39Z")

</div>

Ahhh… that seems to be a vairly simple way, thanks 🙂 I’ll test both ways and check what is better for my needs…

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 19, 2018, 8:03am UTC](https://discourse.julialang.org/t/condition-on-a-whole-array/9148/5 "2018-02-19T08:03:59Z")

</div>

One thing to keep in mind is that `ifelse()` and the `?` operator are actually a bit different because `ifelse` evaluates _both_ of its arguments, while `?` only evaluates the argument chosen by the test. For example:

```julia
julia> function f(x)
         println("calling f")
         1
       end
f (generic function with 1 method)

julia> function g(x)
         println("calling g")
         2
       end
g (generic function with 1 method)

julia> y = true ? f(1) : g(1)
calling f
1

julia> y = ifelse(true, f(1), g(1))
calling f
calling g
1

```

It seems like this would make `ifelse()` always worse, but in fact the predictability of its behavior (always evaluating both arguments) can _sometimes_ allow for more efficient pipelined code.

---

<div class="post-metadata">

**Author:** ![Ferran\_Mazzanti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ferran_mazzanti/32/7458_2.png) [@Ferran\_Mazzanti](https://discourse.julialang.org/u/Ferran_Mazzanti)\
**Post date:** [February 19, 2018, 11:46am UTC](https://discourse.julialang.org/t/condition-on-a-whole-array/9148/6 "2018-02-19T11:46:27Z")

</div>

Oh thanks.

But anyway, I still wonder if there is a ‘dot’ notation for the ?  
operator… Something of the form

x == 0 .? true : false

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [February 19, 2018, 12:48pm UTC](https://discourse.julialang.org/t/condition-on-a-whole-array/9148/7 "2018-02-19T12:48:18Z")

</div>

Regarding performance, you could try [https://github.com/JuliaArrays/MappedArrays.jl](https://github.com/JuliaArrays/MappedArrays.jl).
