Path: news.mathworks.com!not-for-mail From: <HIDDEN> Newsgroups: comp.soft-sys.matlab Subject: Re: conditional operator Date: Tue, 31 Aug 2010 15:46:04 +0000 (UTC) Organization: The MathWorks, Inc. Lines: 26 Message-ID: <i5j83s$cip$1@fred.mathworks.com> References: <ef55682.-1@webcrossing.raydaftYaTP> Reply-To: <HIDDEN> NNTP-Posting-Host: webapp-02-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: fred.mathworks.com 1283269564 12889 172.30.248.37 (31 Aug 2010 15:46:04 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Tue, 31 Aug 2010 15:46:04 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 2469360 Xref: news.mathworks.com comp.soft-sys.matlab:666517 I had the same Problem before, solved it a little bit differently. I made the if statement into a seperate function that would return a logical 1 or 0. Then I multiplied the result of my if-function with the result of my function and added the two results. I.e. let's say y = 2x | x>2 y = -2x | x <= 2 cond = @(x) (x>2) func = @(x) cond(x) * 2x + (1-cond(x)) * -2x "Dietrich Lueerssen" <Dietrich.Lueerssen@REMOVE-THIS.ogt.co.uk> wrote in message <ef55682.-1@webcrossing.raydaftYaTP>... > Hi there fellow MATLABers, > > I have recently had the following "problem": I wanted to define a > function such as the sinc function (sin(x)/x), which for x->0 > approaches 1, but obviously MATLAB throws a division by zero error. > The solution is simple enough (define the function in an m-file, use > an if-statement, and test for the "offending" input). Done that, > works fine. > > Here is my question for the community: ideally I would have liked to > define an anonymous function (sinc = @(x) sin(x)/x;), but that does > not allow the if-statement. In C, I would have used the conditional > operator "?", as in ( (x==0)?1:sin(x)/x ). > I am not aware of MATLAB having such a nice tool; does somebody know > a simple way to achieve the same thing? > > Dietrich