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