conversion to one line function

8 ビュー (過去 30 日間)
Natalia Przedborska
Natalia Przedborska 2020 年 5 月 20 日
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
  1 件のコメント
darova
darova 2020 年 5 月 20 日
I think it's impossible

サインインしてコメントする。

採用された回答

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
  1 件のコメント
Natalia Przedborska
Natalia Przedborska 2020 年 5 月 20 日
thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by