conversion to one line function

8 次查看(过去 30 天)
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

采纳的回答

Bjorn Gustavsson
Bjorn Gustavsson 2020-5-20
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by