# Vector indexing doesn't work

**URL:** <https://discourse.mc-stan.org/t/vector-indexing-doesnt-work/32208>\
**Category:** General\
**Tags:** techniques\
**Created:** [July 24, 2023, 9:14am UTC](https://discourse.mc-stan.org/t/vector-indexing-doesnt-work/32208 "2023-07-24T09:14:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sha](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@sha](https://discourse.mc-stan.org/u/sha)\
**Post date:** [July 24, 2023, 9:14am UTC](https://discourse.mc-stan.org/t/vector-indexing-doesnt-work/32208/1 "2023-07-24T09:14:03Z")

</div>

## Hi everyone, I think this is very simple question but I failed to fix it. I am defining my ODE in stan where I get an error when I assign the first element of pop\_init (which is a vector) to the variable L which is a real. But I get the incompatibility error. Any help is appreciated. Thanks

```
 5: vector[] theta, // parameters
 6: array[] real x_r, array[] int x_i) { // unused
 7: real L = pop_init[1];
         ^
 8: real H = pop_init[2];
 9: real bh = theta[1];

```

* * *

Ill-typed arguments supplied to assignment operator =: lhs has type real and rhs has type vector  
This is the entire stan code:

```no-highlight
functions {
  vector[] MyODE( real t, // time
                vector[] pop_init, // initial states
                vector[] theta,
                array[] real x_r, array[] int x_i) { // unused // parameters
     real L = pop_init[1];
    real H = pop_init[2];
    real bh = theta[1];
    real mh = theta[2];
    real ml = theta[3];
    real bl = theta[4];
    // differential equations
    real dH_dt = (bh - mh * L) * H;
    real dL_dt = (bl * H - ml) * L;
    return { dL_dt , dH_dt };
  }
}
data{
  int N; 
  vector[N] L; 
  vector[N] H; 
}
parameters{
  vector<lower=0>[4] theta; //{bH, mH, bL, mL}
  vector<lower=0>[2] sigma;
}
model{
  matrix[N, 2] pop;
  pop[1,1] = H[1];
  pop[1,2] = L[1];
  vector[20] SEQ;
  for(i in 1:20) SEQ[i] = i; 
  pop[2:N,1:2] = ode_rk45(
    MyODE, pop[1], 0, SEQ, theta,
    rep_array(0.0, 0), rep_array(0, 0),
    1e-5, 1e-3, 5e2);
  for ( t in 2:N ){
    target += lognormal_lpdf(H[t] | log(pop[t, 1]), sigma[1]); 
    target += lognormal_lpdf(L[t] | log(pop[t, 2]), sigma[2]); 
  }
  sigma ~ gamma(2, 30);
  theta[1] ~ gamma(4, 2);
  theta[2] ~ gamma(2, 4);
  theta[3] ~ gamma(3, 2);
  theta[4] ~ gamma(1, 10);
}
'

```

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [July 24, 2023, 2:49pm UTC](https://discourse.mc-stan.org/t/vector-indexing-doesnt-work/32208/2 "2023-07-24T14:49:18Z")

</div>

> [@sha](#):
>
> ` vector[] pop_init, // initial states`

`vector[]` is the (deprecated) equivalent of `array[] vector`. So the line `real L = pop_init[1]` is selecting the first element of an array of vectors, which is a vector, not a real.

---

<div class="post-metadata">

**Author:** ![sha](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@sha](https://discourse.mc-stan.org/u/sha)\
**Post date:** [July 25, 2023, 9:34am UTC](https://discourse.mc-stan.org/t/vector-indexing-doesnt-work/32208/3 "2023-07-25T09:34:12Z")

</div>

Hi, Got it. Thanks :)
