Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: conditional operator
Date: Wed, 20 Mar 2013 22:16:06 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 44
Message-ID: <kidcf6$sc7$1@newscl01ah.mathworks.com>
References: <ef55682.-1@webcrossing.raydaftYaTP> <4635e28a$0$12844$426a34cc@news.free.fr> <f14sdd$ns2$1@fred.mathworks.com> <kiadpj$jd2$1@newscl01ah.mathworks.com> <kicgmk$okd$1@newscl01ah.mathworks.com> <kid2oc$r51$1@newscl01ah.mathworks.com> <kid62m$997$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-03-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1363817766 29063 172.30.248.48 (20 Mar 2013 22:16:06 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 20 Mar 2013 22:16:06 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2460068
Xref: news.mathworks.com comp.soft-sys.matlab:791644

"Steven_Lord" <slord@mathworks.com> wrote in message <kid62m$997$1@newscl01ah.mathworks.com>...

> In the example, note that the second and third inputs to QUESTIONCOLON are 
> _anonymous functions_ not numeric arrays. If you use that syntax yes, MATLAB 
> will need to construct those anonymous functions as part of calling the 
> function. But it will NOT call SIN or perform the division until the last 
> line of the function and will ONLY call SIN and perform the division for 
> those elements of x where (x == 0) is false (i.e. x ~= 0). If all the 
> elements of x are zero, that's going to be a pretty quick calculation. It's 
> not exactly short-circuiting, but it's pretty close.

I see your point -- it should usually be possible to do something like that.  I just don't want to.  :)  I don't want to have to define more anonymous functions like this.  This whole thread is admittedly about syntactic sugar.  Everybody can already do what they want to, the question is, how easily?  If there was a real ternary operator like I described, I would use it _all_ the time!  Not just in function definitions.  I would use it in sections of code where I check parameters, initialize things, etc.  Like this:
  x = isfield(s, 'x') ? s.x : 3;
That's only one of many examples.  Again, I can already do what I need with what MATLAB already has.  I just want it to be nicer looking.  A ternary operator would really help.

> >    X = if A then B else C;
> 
> That's already valid MATLAB syntax if you remove the then and add some 
> commas, though it's not a single expression (and so is not eligible to be 
> used in an anonymous function.)

I'm not sure what you mean there.

> > I believe if the condition returns a nonscalar result, 
> > for an IF statement there is an implicit "any()" around the condition. 
> > You go one way or the other, and only once.
> 
> ALL, not ANY.

Oops, right.

> Given that IF does ALL, I think it's safe to amend your respond to ones(5) 
> since not ALL the elements of A are nonzero.

Right.

> If this hypothetical 
> ternary operator behaved this way, it may be reasonable that if ALL the 
> elements in the condition were true or false we could skip executing the 
> other piece entirely.

Sounds nice!

Jim