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

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).

``````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:

``````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)
``````

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

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.

@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).

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

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

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). It is perfectly fine to ask here, but perhaps investing some time in learning to use the new debugger, or just learning to debug by printing (eg `@show`) would also help.

1 Like