How to set a constant if a parameter matches criteria?

#1

I would like to be able to set something as a constant if conditions are met.

I think sum could be equivalent to colSums in R so I think it would look something like

using Distributions
#Constants
const Ropt = 100
const γ= 2
const NumSpecies = 50

m = sort(rand(Uniform(10^2, 10^12), NumSpecies))

#L is the success curve of consumers aka feeding efficiency, a probability matrix
L = @. (m/(m'*Ropt) * exp(1-m/(m'*Ropt)))^γ
# Weak links (Lij <p) are removed
L[L .<= 0.01] .= 0
L

#take the link probabilities of the previous matrix and determine if L[i,j] > p
FoodWeb = zero(L)
FoodWeb[L .> rand.(Uniform(0, 1))] .= 1 # Sets F[i,j] = 0  wherever  L[i,j] < F[i,j]
FoodWeb

if !(sum(FoodWeb, dims=(1))\equal == 0) & !(sum(FoodWeb, dims =(2))==0)
  return(m = sort(rand(Uniform(10^2, 10^12), NumSpecies)))
else m = const

That is, if a column AND a row in FoodWeb both only have zeros, I want to re-run m = sort(rand(Uniform(10^2, 10^12), NumSpecies until this is no longer the case, and m becomes a const.

What I mean by rows and columns having 0 values:

image

#2

Don’t use global scope, and then it won’t matter if m is const. Put your code into functions.

4 Likes