Need helps with my unit step function

4 次查看(过去 30 天)
La Vang Le
La Vang Le 2018-10-11
编辑: Rik 2018-10-11
So I wanted to create a unit step function that contain a time vector that specifies the finite range of the signal and a time shift value, which is equivalent to u(t + ts). But every time I tried to run the function, it did not work as I expected. The reason I believe it did not work was because of time vector in a function. I do not know how to make a time vector that can set a finite range by inputing any integer. I would be much thankful if anyone can help me. Here is my code I have problem with.
function [y] = unitstep(t, ts)
for i = -t:t
y = zeros(size(i));
if i >= ts
y = 1;
elseif i < ts
y = 0;
end
end

回答(1 个)

Rik
Rik 2018-10-11
编辑:Rik 2018-10-11
Your current code overwrites your output on every iteration. It looks like this is what you want:
function [y] = unitstep(t, ts)
t=-t:t;%only works if t is a scalar
y = zeros(size(t));
for ind = 1:numel(t)
if ind >= ts
y(ind) = 1;
else
y(ind) = 0;
end
end
Which can be simplified to this:
function [y] = unitstep(t, ts)
t=-t:t;%only works if t is a scalar
y=zeros(size(t));
y(y>=ts)=1;

类别

Help CenterFile Exchange 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by