Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

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

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

From   Nick Cox <>
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>


On Fri, Apr 27, 2012 at 7:24 PM, László Sándor <> 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:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index