example-
f(x)= 0 x<0
x 0<x<1
-1 x>1

댓글 수: 2

David Young
David Young 2015년 2월 9일
편집: David Young 2015년 2월 9일
What is supposed to happen if x=0 or x=1? I'm guessing you mean
0 if x < 0
x if 0 <= x <= 1
-1 if x > 1
David Webb
David Webb 2015년 2월 9일
function given above is just for example not the actual function. Just wanted to know how to declare such a function.

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

 채택된 답변

Matt J
Matt J 2015년 2월 8일
편집: Matt J 2015년 2월 8일

0 개 추천

One way,
f=@(x) x.*(x>=0 & x<=1) - (x>1)

댓글 수: 2

David Webb
David Webb 2015년 2월 9일
Can you please explain ... Thanks!!
David Webb
David Webb 2015년 2월 9일
Got it Thanks man !!

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

추가 답변 (2개)

David Young
David Young 2015년 2월 9일

1 개 추천

I would go for a vectorised function:
function x = clipZeroOne(x)
x(x < 0) = 0;
x(x > 1) = -1;
end
Test examples
clipZeroOne(9)
clipZeroOne(0.5)
clipZeroOne(-0.5)
clipZeroOne([-Inf -3 0 0.2 0.9 1 15 Inf]) % apply to a vector
Erik S.
Erik S. 2015년 2월 8일
편집: Guillaume 2015년 2월 9일

0 개 추천

function f=fcn(x)
if x<0
y = 0;
else if x>=0 && x<1
y = x;
else if x>1
y = -1;
end
In the example f(0) = x.

댓글 수: 2

David Webb
David Webb 2015년 2월 9일
Thanks for the answer ..
David Young
David Young 2015년 2월 9일
편집: David Young 2015년 2월 9일
This answer has multiple issues, as they like to say on Wikipedia.
  • What happens if x is exactly equal to 1?
  • The output variable is f but you assign the result to y.
  • "else if" isn't legal MATLAB.
  • The "{} Code" button makes the code display clearly.
I guess what you meant was this
function y = fcn(x)
if x<0
y = 0;
elseif x<1 % it must be greater than or equal to 0
y = x;
else
y = -1; % it must be greater than or equal to 1
end
end

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

카테고리

제품

질문:

2015년 2월 8일

댓글:

2015년 2월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by