Here's a less obvious solution (however, vectorized):
f = @(x) subsasgn(zeros(size(x)), struct('type','()','subs',{{x>=1}}), nan) + 0
Basically its equivalent:
function v = f(x) v = zeros(size(x)); v( x>=1 ) = nan;
+0 at the end - always force output, even if f is called without output arguments (returns to ans ). Example:
>> f(-2:2) ans = 0 0 0 NaN NaN
Amro
source share