Statalist


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

Re: st: RE: ifelse mata function


From   Ben Jann <ben.jann@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: ifelse mata function
Date   Tue, 12 May 2009 15:57:16 +0200

Maybe also have a look at -mm_cond()- from the -moremata- package.
It's a matrix version of Mata's conditional operator.

 . ssc install moremata
 . help mata mm_cond()

It will not be faster than your loop though, I think.
ben

On Tue, May 12, 2009 at 1:29 PM, philippe van kerm
<philippe.vankerm@ceps.lu> wrote:
> See under 'conditional operator':
>
>  help m2_op_conditional (http://www.stata.com/help.cgi?m2_op_conditional)
>
> Philippe
>
>
>> -----Original Message-----
>> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-
>> statalist@hsphsun2.harvard.edu] On Behalf Of Nicola Orsini
>> Sent: Tuesday, May 12, 2009 1:21 PM
>> To: statalist@hsphsun2.harvard.edu
>> Subject: st: ifelse mata function
>>
>> Mata users,
>>
>> 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)
>>
>> Nicola
>>
>>
>>
>> *
>> *   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/
>
> *
> *   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/
>

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



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