# Using MATLAB to define a function in signal processing 101

I want to define a function $y(t)$ that has 2 cases:

$$y(t)= \begin{cases} -1 & \text{for} & -T \lt t \leq 0 \\ 1 & \text{for} & 0 \lt t\leq T \end{cases}$$ where $T$ is an integer.

I already defined $t$ using linspace between $-T$ to $T$ in a resolution of 1000 points.

I was advised to use the command ones, but I dont know how to apply it by the function terms.

Thank you.

• y=[-ones(500,1);ones(500,1)]; Apr 6, 2015 at 18:01
• Thank you, it worked. I just dont understand how does it work, because when I plotted (t,y) it gave me exactly what I wanted, but ones(500,1) gives you a vector of 500 1's, and it has nothing to do with t. Im just trying to understand this better. Apr 7, 2015 at 5:27
• You're right, y has nothing to do with t, just the lengths of the two vectors must be the same. y simply gives you the values, and t defines where on the t-axis these values are located. That's the way it works. Apr 7, 2015 at 6:58
• Why not to use the sign function and set the value for t=0 to be 1?
– jojek
Jun 6, 2015 at 13:34

Well, this is Octave but should work in Matlab:

function y=Y(t)
y=ones(size(t));
y=y+(t<=0)*(-2);


.. and it is vectorised. Calling will look like:

>t=linspace(-T,T,1000);
>y=Y(t);


To make this more general we could also make the time at which the function switches a parameter also:

function y=Y(t,S)
y=ones(size(t));
y=y+(t<=S)*(-2);