# ERROR: TypeError: non-boolean (Array{Float64,2}) used in boolean context

**URL:** <https://discourse.julialang.org/t/error-typeerror-non-boolean-array-float64-2-used-in-boolean-context/23543>\
**Category:** New to Julia\
**Created:** [April 26, 2019, 9:30am UTC](https://discourse.julialang.org/t/error-typeerror-non-boolean-array-float64-2-used-in-boolean-context/23543 "2019-04-26T09:30:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [April 26, 2019, 9:30am UTC](https://discourse.julialang.org/t/error-typeerror-non-boolean-array-float64-2-used-in-boolean-context/23543/1 "2019-04-26T09:30:32Z")

</div>

Julia produces an error when (I think) I specify `if b[:,1:NumPlantSpecies]`.

I want to specify different values for `b0` and `β` when **row** values are between 1 and the number of plant species (if statement), and between the number of plant species+1 and number of total species (else statement).

```julia
function create_b(NumTotalSpecies, log10BM, L)
    b = zeros(NumTotalSpecies, NumTotalSpecies)
    for i = 1:NumTotalSpecies
        for j = 1:NumTotalSpecies
            if b[:,1:NumPlantSpecies] #indicates herbivory
                b0 = 1000 
                β = 0.19 #NOTE how to add ± 0.04 ?
                permutedims(log10BM) = 1 # here (log10BM'^β[i]) * L should = 1
                L = 1 # here (log10BM'^β[i]) * L should = 1
            else #everything else is omnivore or carnivore
                b0 = 50 
                β = 0.42 #NOTE how to add ± 0.05 ?
            end
            b[i,j] = b0 * (log10BM[i]^β) * (log10BM[j]^β) * L[i, j]
        end
    end
    return(b)
end

b = create_b(NumTotalSpecies, log10BM, L)

```

I understand that Julia was expecting a boolean value (true/false) but I don’t understand why `1:NumPlantSpecies` is not a clear boolean criteron.

Necessary code is:

```julia
using Distributions, Random

Ropt = 100
γ= 2
q = rand(Normal(0.5,0.2))#Hill exponent, μ = 0.5, σ = 0.2 "within in. limits of 0 and 1"
#rand()is between 0 and 1, unless specified

NumPlantSpecies = 8
NumAnimalSpecies = 12
NumTotalSpecies = NumPlantSpecies + NumAnimalSpecies

log10BM_P = sort(rand(Uniform(10^0, 10^6), NumPlantSpecies))
log10BM_A = sort(rand(Uniform(10^2, 10^12), NumAnimalSpecies)) 
log10BM = vcat(log10BM_P,log10BM_A) 

function create_FoodWeb(log10BM,Ropt,γ) 
    function create_L(log10BM,Ropt,γ) 
        L = @. ((log10BM/(log10BM'*Ropt))' * exp(1-(log10BM/(log10BM'*Ropt))'))^γ
        # Weak links (Lij <p) are removed
        L[L .<= 0.01] .= 0.0
        L[:,1:NumPlantSpecies] .= 0.0
        return(L)
    end

    condition = true
    while condition
        log10BM_P = sort(rand(Uniform(10^0, 10^6), NumPlantSpecies))
        log10BM_A = sort(rand(Uniform(10^2, 10^12), NumAnimalSpecies))
        log10BM = vcat(log10BM_P,log10BM_A)

        global L = create_L(log10BM,Ropt,γ)
        #take the link probabilities of the previous matrix and determine if L[i,j] > p
        global FoodWeb = zero(L)
        FoodWeb[:,1:NumPlantSpecies] .= 0.0 # keeps plants as basal species
        FoodWeb[L .> rand.(Uniform(0, 1))] .= 1.0 # Sets F[i,j] = 0 wherever L[i,j] < F[i,j]

        condition = any(sum(x->x>0, FoodWeb, dims=1) + sum(x->x>0, FoodWeb, dims=2)' .== 0.0)
        #dims=(1) for column, dims=(2) for row
    end
    return(log10BM, L, FoodWeb)
end

log10BM,L,FoodWeb = create_FoodWeb(log10BM, Ropt,γ)

function create_b(NumTotalSpecies, log10BM, L)
    b = zeros(NumTotalSpecies, NumTotalSpecies)
    for i = 1:NumTotalSpecies
        for j = 1:NumTotalSpecies
            if b[:,1:NumPlantSpecies] #indicates herbivory
                b0 = 1000 
                β = 0.19 #NOTE how to add ± 0.04 ?
                permutedims(log10BM) = 1 # here (log10BM'^β[i]) * L should = 1
                L = 1 # here (log10BM'^β[i]) * L should = 1
            else #everything else is omnivore or carnivore
                b0 = 50 
                β = 0.42 #NOTE how to add ± 0.05 ?
            end
            b[i,j] = b0 * (log10BM[i]^β) * (log10BM[j]^β) * L[i, j]
        end
    end
    return(b)
end

b = create_b(NumTotalSpecies, log10BM, L)

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 26, 2019, 9:33am UTC](https://discourse.julialang.org/t/error-typeerror-non-boolean-array-float64-2-used-in-boolean-context/23543/2 "2019-04-26T09:33:46Z")

</div>

I think you are right about the source of the error. Can’t you just condition on `b[i, j]`?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [April 26, 2019, 9:46am UTC](https://discourse.julialang.org/t/error-typeerror-non-boolean-array-float64-2-used-in-boolean-context/23543/3 "2019-04-26T09:46:20Z")

</div>

> [@HelgavonLichtenstein](#):
>
> I don’t understand why `1:NumPlantSpecies` is not a clear boolean criteron.

I’m not sure I understand - how would `1:NumPlantSpecies` (which in your case just seems to be a `UnitRange` object, `1:8`) be a boolean criterion, i.e. in what sense can the range 1:8 be “true” or “false”?

Also, the error is not complaining about the range object per se (although as I said I don’t see how that would work as a boolean either), but about the array you’re using in the `if` condition - doing `b[:, 1:NumPlantSpecies]` returns an array with `NumTotalSpecies` (20) rows and `NumPlantSpecies` (8) columns.

Again the question is in what sense would this array (which at the start of your loop only holds zeros, and is being filled with floating point numbers) be “true” or “false”? I don’t understand your code well enough to figure out what the right way to test for whether something is a herbivore is, but it seems to me like you need some sort of condition here like e.g. `if sum(b[:, 1:NumPlantSpecies]) ≈ 0.0` that returns a bool.

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [April 26, 2019, 9:55am UTC](https://discourse.julialang.org/t/error-typeerror-non-boolean-array-float64-2-used-in-boolean-context/23543/4 "2019-04-26T09:55:07Z")

</div>

@Tamas_Papp What do you mean by condition on `b[i,j]`?

@nilshg My thinking is that if the rows of j fall between `1:NumPlantSpecies`, this would fulfill the if criteria (i.e. be true).

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [April 26, 2019, 10:00am UTC](https://discourse.julialang.org/t/error-typeerror-non-boolean-array-float64-2-used-in-boolean-context/23543/5 "2019-04-26T10:00:53Z")

</div>

> [@HelgavonLichtenstein](#):
>
> My thinking is that if the rows of j fall between `1:NumPlantSpecies` , this would fulfill the if criteria (i.e. be true).

Then try one of the following (mostly depending on stylistic taste)

```julia
if j in 1:NumPlantSpecies
if 1 <= j <= NumPlantSpecies
if j <= NumPlantSpecies

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 26, 2019, 10:23am UTC](https://discourse.julialang.org/t/error-typeerror-non-boolean-array-float64-2-used-in-boolean-context/23543/6 "2019-04-26T10:23:36Z")

</div>

I think you need the solution suggested by @GunnarFarneback.

Generally though, you seem to be having difficulties with understanding and debugging your code (eg [this problem yesterday](https://discourse.julialang.org/t/error-methoderror-no-method-matching-float64-normal-float64/23509/3)). It is perfectly fine to ask here, but perhaps investing some time in learning to use the [new debugger](https://julialang.org/blog/2019/03/debuggers), or just learning to debug by printing (eg `@show`) would also help.
