# Vectorized if-else?

**URL:** <https://discourse.mc-stan.org/t/vectorized-if-else/9373>\
**Category:** Modeling\
**Created:** [June 24, 2019, 11:05am UTC](https://discourse.mc-stan.org/t/vectorized-if-else/9373 "2019-06-24T11:05:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiagocc](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tiagocc/32/85_2.png) [@tiagocc](https://discourse.mc-stan.org/u/tiagocc)\
**Post date:** [June 24, 2019, 11:05am UTC](https://discourse.mc-stan.org/t/vectorized-if-else/9373/1 "2019-06-24T11:05:40Z")

</div>

Hi everyone,

I am trying to vectorize a function on the `functions` block. However, at some point I need to make a decision based on an elementwise comparison. Something like:

```stan
vector foo(vector y) {
real quantity;
int N = num_elements(y);
vector[N] z = if (y < quantity) {
do_something(y);
} else {
do_nothing(y);
}
return z;
}

```

Is there a workaround to implement this sort of thing in Stan? Or at the moment the only way to go would be to use a for loop on every element of my vector?

Thanks,  
Tiago

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [June 24, 2019, 1:43pm UTC](https://discourse.mc-stan.org/t/vectorized-if-else/9373/2 "2019-06-24T13:43:07Z")

</div>

Loop

---

<div class="post-metadata">

**Author:** ![tiagocc](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tiagocc/32/85_2.png) [@tiagocc](https://discourse.mc-stan.org/u/tiagocc)\
**Post date:** [June 25, 2019, 6:54am UTC](https://discourse.mc-stan.org/t/vectorized-if-else/9373/3 "2019-06-25T06:54:01Z")

</div>

Thanks!
