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 &amp; \text{for} &amp; -T \lt t \leq 0 \\
1 &amp; \text{for} &amp; 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.
Well, this is Octave but should work in Matlab:

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


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

&gt;t=linspace(-T,T,1000);
&gt;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&lt;=S)*(-2);