x=-2:0.1:2; y = (x.^2-1).*(x>=1)+0.*(x>-1&x<1)+(-x.^2-1).*(x<=-1); plot(x,y,'r-p')
追问
If结构呀
追答
x=-2:0.1:2; y = zeros(size(x)); for i =1:1:numel(x) if(x(i)<-1) y(i) =-x(i)^2-1; elseif(x(i)>-1&&x(i)<1) y(i) = 0; else y(i) = x(i)^2-1; end plot(x,y,'r-p')