Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: mata conditional operator element-by-element?


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: mata conditional operator element-by-element?
Date   Sat, 28 Apr 2012 11:10:31 +0100

Mata does not support ? : for arrays.

Ben Jann offers an -mm_cond()- in his -moremata- (SSC). It won't be
(can't be) more efficient than the most direct way of working around
the limitation above. It might be more convenient.

In my experience work-arounds usually boil down to

<vector or matrix yielding 1s if true> :* <result if true>
:+
<vector or matrix yielding 1s if false> :* <result if false>

Nick

On Fri, Apr 27, 2012 at 7:24 PM, László Sándor <sandorl@gmail.com> wrote:

> I checked the help for -m2_op_conditional- on up-to-date Stata 12.1 MP
> for mac, and it looks like the conditional operator only works for
> "general conditions", i.e. scalars, not element-by-element in vectors.
> Much of my code did use -st_select-, but I thought some parts could
> still be cleaner and/or more efficient if I could use something like
> the conditional operator for vectors (as Matlab does allow). Please
> let me know if you have good advice how to get from this
>
> (t:/psc:^2 + (-1:*t:+1):/(-1:+psc):^2))
>
> to something like this
>
> (t == 1 ? psc : psc:-1)^2
>
> [Yes, I would immediately see a problem with this syntax a colon
> operator confusing the conditional operator…]
>

*
*   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   |   Site index