# Defining piecewise function in matlab

by tmpr
Tags: defining, function, matlab, piecewise
 P: 5 How would you write piecewise functions in matlab that can take vector inputs? Here's a function that I'm trying to write. function y=g(x) if x==0 y=1; else y=sin(x)./x; end If I call g([0,pi/2]), I want it to return [0,2/pi], but what I get instead is [NaN,2/pi]. I'm guessing when I write x==0, matlab is comparing the entire input to 0.