conversion to one line function

8 views (last 30 days)
Hello,
I was wondering, if there is a possibility to convert my code into one line function. I need to find odd and even parts of this function, but I don't know how, if my funtion looks like that.
Here is my code:
t = -10 : 0.01 : 10;
x = zeros(size(t))
x(t>=-1 & t<1) = 3;
x(t>=1 & t<2)= -5.*t(t>=1 & t<2)+ 12;
x(t>=2 & t<=4)= -1.*t(t>=2 & t<=4)+ 4;
figure
plot(t,x, 'LineWidth' ,2);
xlabel('t')
ylabel('x(t)')
title('My signal')
grid on

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 20 May 2020
You can build an one-line anonymous function like this:
oneliner = @(t) 3.*double(-1 <= t & t<1) + (-5.*t+12).*double(1<= t & t<2);
t = -10 : 0.01 : 10;
plot(t,oneliner(t))
You'll have to finish it up, but it is just to add the different piece-wise components one by one.
HTH

More Answers (0)

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!