[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
"Nick Cox" <[email protected]> |

To |
<[email protected]> |

Subject |
st: RE: RE: RE: ifelse mata function |

Date |
Tue, 12 May 2009 21:11:29 +0100 |

I usually end up doing something like Philippe's example when I want an equivalent to :? that produces a vector or matrix result. Note that Nicola's particular example is not very convincing as abs(y - x) gets you there in one. If abs() didn't exist rowmax((x,y)) - rowmin((x,y)) would be another way to do it. But Nicola's wider question remains. I don't know if there's a technical question why :? doesn't extend beyond scalar results. Nick [email protected] philippe van kerm Sorry, I realize the conditional operator will not help as it only takes a scalar as condition. Then, one can probably do the switch 'manually' with something like z = (y:>=x) :* (y:-x) + (1:-(y:>=x)) :* (x:-y) philippe van kerm > See under 'conditional operator': > > help m2_op_conditional > (http://www.stata.com/help.cgi?m2_op_conditional) Nicola Orsini > > do you know any mata function similar to ifelse implemented in R or > > the cond() programming function in Stata? > > > > > http://www.iiap.res.in/astrostat/School07/R/library/base/html/ifelse.ht > > ml > > > > I know I can loop across observations but it's not really fast with > > large dataset in an optimization process where this process has to be > > repeated many times. > > > > Here is a simple example > > > > mata > > mata clear > > y = runiform(10,1) > > x = runiform(10,1) > > z = J(10,1,.) > > for (i=1; i<=rows(y); i++) { > > if (y[i]>= x[i]) z[i] = y[i]-x[i] > > else z[i] = x[i]-y[i] > > } > > y, x, z > > end > > > > Wouldn't be great to have a mata function like -ifelse-? > > > > ifelse(y>=x, z=y-x, z=x-y) * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**References**:**st: ifelse mata function***From:*Nicola Orsini <[email protected]>

**st: RE: ifelse mata function***From:*philippe van kerm <[email protected]>

**st: RE: RE: ifelse mata function***From:*philippe van kerm <[email protected]>

- Prev by Date:
**st: COURSE: Multilevel & Longitudinal Modelling 23/24 June, London** - Next by Date:
**st: RE: Re: Re: Data management** - Previous by thread:
**st: RE: RE: ifelse mata function** - Next by thread:
**Re: st: RE: ifelse mata function** - Index(es):

© Copyright 1996–2024 StataCorp LLC | Terms of use | Privacy | Contact us | What's new | Site index |