conversion to one line function

80 visualizaciones (últimos 30 días)
Natalia Przedborska
Natalia Przedborska el 20 de Mayo de 2020
Comentada: Natalia Przedborska el 20 de Mayo de 2020
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

Respuesta aceptada

Bjorn Gustavsson
Bjorn Gustavsson el 20 de Mayo de 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

Más respuestas (0)

Categorías

Más información sobre Signal Radiation and Collection en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by