define function in one line

조회 수: 20 (최근 30일)
fumio hakamada
fumio hakamada 2024년 2월 23일
댓글: Rik 2024년 2월 23일
How can I define vector p without using function statement?
suppose
t = 0:0.1:1
p = sin(t) when t<0.6
p = 0 when t>=0.6
  댓글 수: 2
DGM
DGM 2024년 2월 23일
편집: DGM 2024년 2월 23일
What's wrong with just
t = 0:0.1:1;
p = sin(t).*(t<0.6);
plot(t,p)
... of course, the range and resolution of t makes this plot appear a bit unclear.
fumio hakamada
fumio hakamada 2024년 2월 23일
Thank you for your reply.

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2024년 2월 23일
I don't know if you want to avoid anonymous functions as well, but this should give you a finer plot:
% p = sin(t) when t<0.6
% p = 0 when t>=0.6
p = @(t) (t<0.6).*sin(t);
fplot(p,[0 1])
The problem here is mostly that sin(x) is very close to x for small x, which means you have a fairly straight line. Adding a straight line helps to show there is actually a slight curve:
p = @(t) (t<0.6).*sin(t);
figure
fplot(p,[0 1])
hold on
fplot(@(t) sign(p(t)).*t,[0 1])
legend({'y = sin(t)','y = x'})
  댓글 수: 2
fumio hakamada
fumio hakamada 2024년 2월 23일
이동: Rik 2024년 2월 23일
I had no idea about logical vector and usage of ".*". Thanks for both of you.
Rik
Rik 2024년 2월 23일
You're welcome. If my answer solved your issue, please consider marking it as accepted.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Digital Filtering에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by