# How to set a constant if a parameter matches criteria?

**URL:** <https://discourse.julialang.org/t/how-to-set-a-constant-if-a-parameter-matches-criteria/21055>\
**Category:** New to Julia\
**Created:** [February 21, 2019, 4:23pm UTC](https://discourse.julialang.org/t/how-to-set-a-constant-if-a-parameter-matches-criteria/21055 "2019-02-21T16:23:04Z")\
**Posts on this page:** 2\
**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:** [February 21, 2019, 4:23pm UTC](https://discourse.julialang.org/t/how-to-set-a-constant-if-a-parameter-matches-criteria/21055/1 "2019-02-21T16:23:04Z")

</div>

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

I think [sum](https://docs.julialang.org/en/v1/base/collections/#Base.sum) could be equivalent to `colSums` in R so I think it would look something like

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/d/9/d9b2653f8da8749a6a17efddfdf1e0525f856cfc.png)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 21, 2019, 4:34pm UTC](https://discourse.julialang.org/t/how-to-set-a-constant-if-a-parameter-matches-criteria/21055/2 "2019-02-21T16:34:00Z")

</div>

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