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

st: ifelse mata function

From   Nicola Orsini <>
Subject   st: ifelse mata function
Date   Tue, 12 May 2009 13:20:40 +0200

Mata users,

do you know any mata function similar to ifelse implemented in R or the cond() programming function in Stata?

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 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

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:

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