conversion to one line function
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Natalia Przedborska
il 20 Mag 2020
Commentato: Natalia Przedborska
il 20 Mag 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
Risposta accettata
Bjorn Gustavsson
il 20 Mag 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
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Function Creation in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!