# How compare two vectors in conditional

**URL:** <https://discourse.julialang.org/t/how-compare-two-vectors-in-conditional/72747>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 8, 2021, 5:24am UTC](https://discourse.julialang.org/t/how-compare-two-vectors-in-conditional/72747 "2021-12-08T05:24:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sbeltranj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbeltranj/32/32827_2.png) [@Sbeltranj](https://discourse.julialang.org/u/Sbeltranj)\
**Post date:** [December 8, 2021, 5:24am UTC](https://discourse.julialang.org/t/how-compare-two-vectors-in-conditional/72747/1 "2021-12-08T05:24:24Z")

</div>

Hi, I want to make the comparison between two vectors, it must have the condition that a \< 0 and b \< 0, this is my code:

```julia
a = vec(rand(8,1))
b = vec(rand(8,1))

if a .< 0 && b .< 0 #compare two vector
    a = 0
    b = 1
end

```

And I get this error:

```julia
ERROR: LoadError: TypeError: non-boolean (BitVector) used in boolean context
Stacktrace:
 [1] top-level scope

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 8, 2021, 5:26am UTC](https://discourse.julialang.org/t/how-compare-two-vectors-in-conditional/72747/2 "2021-12-08T05:26:52Z")

</div>

do you want `all(a .< 0)`? (or the slightly more efficient `all(x->x>0)`)?

---

<div class="post-metadata">

**Author:** ![Sbeltranj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbeltranj/32/32827_2.png) [@Sbeltranj](https://discourse.julialang.org/u/Sbeltranj)\
**Post date:** [December 8, 2021, 5:38am UTC](https://discourse.julialang.org/t/how-compare-two-vectors-in-conditional/72747/3 "2021-12-08T05:38:29Z")

</div>

Thanks, I did not know this command. This solved my problem.

```julia
a = vec(rand(8,1))
b = vec(rand(8,1))

if all(a .> 0) && all(b .> 0) #compare two vector
    global a,b
    a = 0
    b = 1
end

```

---

<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:** [December 8, 2021, 6:39am UTC](https://discourse.julialang.org/t/how-compare-two-vectors-in-conditional/72747/4 "2021-12-08T06:39:18Z")

</div>

> [@Sbeltranj](#):
>
> ```julia
> a = vec(rand(8,1))
> b = vec(rand(8,1))
> 
> ```

Instead of the above, you can write

```julia
a = rand(8)
b = rand(8)

```

As for the rest of your code,

> [@Sbeltranj](#):
>
> ```julia
> if all(a .> 0) && all(b .> 0) #compare two vector
> global a,b
> a = 0
> b = 1
> end
> 
> ```

changing `a` and `b` from vectors to scalars, is a bad idea, and will make your code slow and unpredictable. Changing the type in a variable depending on values creates a type instability, and there is no way for the compiler to predict what types of values will be held in those variables.

Global variables should also be avoided, and it is recommended that code should be inside functions.
