# My matrix is actually a vector?

**URL:** <https://discourse.julialang.org/t/my-matrix-is-actually-a-vector/19787>\
**Category:** New to Julia\
**Created:** [January 18, 2019, 2:25pm UTC](https://discourse.julialang.org/t/my-matrix-is-actually-a-vector/19787 "2019-01-18T14:25:24Z")\
**Posts on this page:** 10\
**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:** [January 18, 2019, 2:25pm UTC](https://discourse.julialang.org/t/my-matrix-is-actually-a-vector/19787/1 "2019-01-18T14:25:24Z")

</div>

I am missing something as what I had hoped was a matrix seems to be a vector. Continuing from my topic [here](https://discourse.julialang.org/t/building-an-allometric-food-web-help-with-matrices/19709)

My code is

```julia
using Distributions
#Constants
Ropt = 100
γ= 2
k = 8.6173324e-5 #Boltzman eV/K
T0 = 273.15 #K
Temp = 293.15 # example of 20 C degrees
q = 1.2 #Hill exponent

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

#L is the success curve of consumers aka feeding efficiency, a probability matrix
L = @. (m/m'*Ropt * exp(1-m/m'*Ropt))^γ # The @. macro here broadcasts the following scalar expression across `m` and `m'`
# Weak links (Lij <p) are removed
L[L .< 0.01] .= 0 # . lets you do things element by element in an array

#create a matrix of links:
#take the link probabilities of the previous matrix and determine if L[i,j] > p
FoodWeb = rand(Uniform(0,1), size(L)...) #what does ... do? This is impossible to search in the Julia documentation
FoodWeb[L .> FoodWeb] .= 1 # Sets F[i,j] = 0 wherever L[i,j] < F[i,j]
FoodWeb

function ParameterMatrix(m, I, b, c, E)
    x = exp(I)*m^b*m'^c*exp((E*(T0 - Temp))/(k*T0*Temp))
end

#attack rate a (m^2/s)
a = @. ParameterMatrix(m, -13.1, 0.25, -0.8, -0.38)

```

where

```julia
julia> a
50-element Array{Float64,1}:

```

instead of being a 50x50 matrix. What am I doing incorrectly?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 18, 2019, 2:29pm UTC](https://discourse.julialang.org/t/my-matrix-is-actually-a-vector/19787/2 "2019-01-18T14:29:05Z")

</div>

`m` is a Vector so why is that strange?

---

<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:** [January 18, 2019, 2:48pm UTC](https://discourse.julialang.org/t/my-matrix-is-actually-a-vector/19787/3 "2019-01-18T14:48:21Z")

</div>

It is strange because `a` is supposed to be a matrix, which shows the attack rate of species with log mass `mi` on species of log mass `mj`. It is a matrix in the paper I am using so I must have made a mistake somewhere.

Here is what the code looks like in R.

```julia
# generate the log body mass values for each species (log of mi values) 
logBM = runif(50, 10^2, 10^12) # animal body mass 10^2 - 10^12
# sort values 
logBM = sort(logBM)

#create a matrix of link probabilities:
#animals of a certain size MAY eat animals of a particular size, but it is not certain
create.matrix.L <- function(m){ 
  number.of.species = length(m)
  Ropt <-100
  gamma <-2
  matrix.L <- matrix(0, nrow = number.of.species, ncol = number.of.species)
  for(j in 1:number.of.species)
  {
    for (i in 1:number.of.species)              
    {
      matrix.L[i,j] <- (m[i]/(m[j]*Ropt)*exp(1-m[i]/(m[j]*Ropt)))^gamma #Food web Formula from Schneider paper
      if (matrix.L[i,j]<=0.01) #weak links where Lij <= 0.01 were removed 
      {
      matrix.L[i,j] = 0.0
      }
    }
  }
  
  return(matrix.L)
}

# create the L matrix:
L <- create.matrix.L(logBM)

#create a matrix of links:
#take the link probabilities of the previous matrix and determine if L[i,j] > p
create.FW <- function(L){ 
  number.of.species = dim(L)[1]
  mat.of.FW <- matrix(0, nrow = number.of.species, ncol = number.of.species)
  for (j in 1:number.of.species)
  {
    for (i in 1:number.of.species)
    {
      p = runif(1,0,1) #n, min, max
      if (L[i,j]>p) 
      {
        mat.of.FW[i,j] = 1 
      }
    }
  }
  return(mat.of.FW)
}

# create the food web matrix:
food.web <- create.FW(L)

#Constants
k <- 8.6173324e-5 #Boltzman eV/K
T0 <- 273.15 #K
Temp <- 293.15 # example of 20 C degrees

#recreate parameters from paper

create.param.matrix <- function(food.web, m, I, b, c, E){ 
  #FW (to match food web generated), m (bodymass), I (intercept), b (body scaling species i)
  #c (body mass scaling Pred), E (Activation energy)
  number.of.species = length(m)
  param.matrix <- matrix(0, nrow = number.of.species, ncol = number.of.species)
  for (j in 1:ncol(param.matrix))
  {
    for (i in 1:nrow(param.matrix))
    {
      param.matrix[i,j] <- exp(I)*m[i]^b*m[j]^c*exp((E*(T0 - Temp))/(k*T0*Temp)) * food.web[i,j]
    } 
  }
  return(param.matrix)
}

#attack rate a (m^2/s)
matrix.a = create.param.matrix(food.web, logBM, -13.1, 0.25, -0.8, -0.38) #I, b, c, E

```

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [January 18, 2019, 4:19pm UTC](https://discourse.julialang.org/t/my-matrix-is-actually-a-vector/19787/4 "2019-01-18T16:19:14Z")

</div>

> [@HelgavonLichtenstein](#):
>
> FoodWeb = rand(Uniform(0,1), size(L)…) #what does … do?

In your case `size(L)` is `(50,50)`.  
`...` used when calling a function will take the elements of the last input parameter and treat them as individual input parameter values.  
`rand(Uniform(0,1), size(L)...) ` is interpreted as `rand(Uniform(0,1), 50, 50)`

> [@HelgavonLichtenstein](#):
>
> What am I doing incorrectly?

`a = @. ParameterMatrix(m, -13.1, 0.25, -0.8, -0.38)` will apply function `ParameterMatrix` to the individual elements of `m`. Therefore the result will be an array.

Instead, consider the following:

```julia
function ParameterMatrix(m, I, b, c, E)
    x = @. exp(I)*m^b*m'^c*exp((E*(T0 - Temp))/(k*T0*Temp))
end

a = ParameterMatrix(m, -13.1, 0.25, -0.8, -0.38)

```

---

<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:** [January 20, 2019, 12:59pm UTC](https://discourse.julialang.org/t/my-matrix-is-actually-a-vector/19787/5 "2019-01-20T12:59:28Z")

</div>

Thank you for your reply, I understand a bit more now. I have changed the code so that it also multiplies with the FoodWeb matrix as I am only interested in the species where L \>= FoodWeb

```julia
function ParameterMatrix(m, I, b, c, E, FoodWeb)
    #FoodWeb (to match food web generated), m (bodymass), I (intercept), b (body scaling species i)
  #c (body mass scaling Pred), E (Activation energy)
    x = @. (exp(I)*(m^b)*(m'^c)*exp(E*((T0 - Temp)/(k*T0*Temp)))).*FoodWeb

end

```

I have another function which is supposed to generate a vector this time, but generates an error. I think the error is telling me that I cannot multiply a vector by a matrix, but am not sure.

The error is

```julia
K = ParameterVector(m, 10, 0.28, 0.71)
ERROR: MethodError: no method matching ParameterVector(::Array{Float64,1}, ::Int64, ::Float64, ::Float64)
Closest candidates are:
  ParameterVector(::Any, ::Any, ::Any, ::Any, ::Any) at none:3
Stacktrace:
 [1] top-level scope at none:0

```

And comes from

```julia
function ParameterVector(m, I, b, E, FoodWeb)
    x = @.(exp(I)*(m^b)*exp(E*((T0 - Temp)/(k*T0*Temp)))).*FoodWeb
end
#Carrying capacity K (g/m2)
K = ParameterVector(m, 10, 0.28, 0.71)

```

---

<div class="post-metadata">

**Author:** ![Karajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karajan/32/8545_2.png) [@Karajan](https://discourse.julialang.org/u/Karajan)\
**Post date:** [January 20, 2019, 1:11pm UTC](https://discourse.julialang.org/t/my-matrix-is-actually-a-vector/19787/6 "2019-01-20T13:11:20Z")

</div>

It looks like your call doesn’t match the function: you are giving 4 parameters:

```julia
K = ParameterVector(m, 10, 0.28, 0.71)

```

but it should be 5:

```julia
ParameterVector(m, I, b, E, FoodWeb)

```

---

<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:** [January 20, 2019, 1:16pm UTC](https://discourse.julialang.org/t/my-matrix-is-actually-a-vector/19787/7 "2019-01-20T13:16:46Z")

</div>

Thank you for your reply,  
I thought that if something is in the "global environment’ like FoodWeb, it does not have to be specified within the function, similar to

```julia
function ParameterMatrix(m, I, b, c, E, FoodWeb)
    x = @. (exp(I)*(m^b)*(m'^c)*exp(E*((T0 - Temp)/(k*T0*Temp)))).*FoodWeb
end

#attack rate a (m^2/s)
a = ParameterMatrix(m, -13.1, 0.25, -0.8, -0.38)

```

---

<div class="post-metadata">

**Author:** ![Karajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karajan/32/8545_2.png) [@Karajan](https://discourse.julialang.org/u/Karajan)\
**Post date:** [January 20, 2019, 1:30pm UTC](https://discourse.julialang.org/t/my-matrix-is-actually-a-vector/19787/8 "2019-01-20T13:30:29Z")

</div>

Not an expert, but I don’t think it works that way. What you just showed shouldn’t work but I suspect it still does because you once had a function with only 5 arguments (instead of the 6 it has now) and Julia uses the old one because that one matches to your function call.

To achieve what you are looking for you could define the global `FoodWeb` as the default for the argument:

```julia
function ParameterMatrix(m, I, b, c, E, FoodWeb = FoodWeb)

```

---

<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:** [January 20, 2019, 1:48pm UTC](https://discourse.julialang.org/t/my-matrix-is-actually-a-vector/19787/9 "2019-01-20T13:48:48Z")

</div>

Thank you, I have placed `FoodWeb = FoodWeb` into the function argument and the error has disappeared.

---

<div class="post-metadata">

**Author:** ![Karajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karajan/32/8545_2.png) [@Karajan](https://discourse.julialang.org/u/Karajan)\
**Post date:** [January 20, 2019, 8:00pm UTC](https://discourse.julialang.org/t/my-matrix-is-actually-a-vector/19787/10 "2019-01-20T20:00:43Z")

</div>

You’re welcome!

If happen to be new to Julia and you are at all concerned about the performance of your code I would recommend you take a look at the [performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/index.html).  
Specifically it would be important to declare your `const`s as such or give them as parameters to your functions to avoid any `global`s.
