
conditional operator
Posted:
Apr 30, 2007 7:43 AM


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 mfile, use an ifstatement, 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 ifstatement. 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

