Matlab 使用if-else条件绘制单位步长

Matlab 使用if-else条件绘制单位步长,matlab,Matlab,我用这段代码来绘制单位阶跃函数 t1=-2:0.01:2; if t1>=0 y=1; else if t1<0 y=0; end end subplot(3,1,1) plot(t1,y)` t1=-2:0.01:2; 如果t1>=0 y=1; 否则,如果t1您的代码一次测试所有t1值,并且与 if all(t1>=0) y=1; else if all(t1<0) y=0; end end 另一种(效率较低)

我用这段代码来绘制单位阶跃函数

t1=-2:0.01:2;

if t1>=0
    y=1;
else if t1<0
    y=0;

   end

end

subplot(3,1,1)
plot(t1,y)`
t1=-2:0.01:2;
如果t1>=0
y=1;

否则,如果t1您的代码一次测试所有t1值,并且与

if all(t1>=0)
    y=1;
else if all(t1<0)
    y=0;

   end

end
另一种(效率较低)方法是:

t1=-2:0.01:2;
for index=1:length(t1)
    if t1(index)>=0
        y(index)=1;
    else if t1(index)<0
            y(index)=0;
        end
    end
end

subplot(3,1,1)
plot(t1,y)
t1=-2:0.01:2;
对于索引=1:长度(t1)
如果t1(索引)>=0
y(指数)=1;

否则,如果t1(index)您的代码一次测试所有t1值,并且与

if all(t1>=0)
    y=1;
else if all(t1<0)
    y=0;

   end

end
另一种(效率较低)方法是:

t1=-2:0.01:2;
for index=1:length(t1)
    if t1(index)>=0
        y(index)=1;
    else if t1(index)<0
            y(index)=0;
        end
    end
end

subplot(3,1,1)
plot(t1,y)
t1=-2:0.01:2;
对于索引=1:长度(t1)
如果t1(索引)>=0
y(指数)=1;

否则,如果t1(索引)您忘记了
heaviside
您忘记了
heaviside
FYI,那么
else if
应该是
elseif
-那么您可以删除额外的
end
。FYI,如果
应该是
elseif
-那么您可以删除额外的
end