There may be "cleaner" ways to do this but one possibility would be:
g = @(x) [f(x) 1]*sparse(1+isnan(f(x)),1,1,2,1);
EDIT: and as others have pointed out the problem with your original g function is that, when f(x) is NaN, you get 0*NaN + 1*1 which still evaluates to NaN...
2 Comments
John (view profile)
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/169709-substituting-a-number-for-nan-in-anonymous-function#comment_260409
Guillaume (view profile)
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/169709-substituting-a-number-for-nan-in-anonymous-function#comment_260418
Sign in to comment.