필터 지우기
필터 지우기

How to formulate the following conditional function?

조회 수: 2 (최근 30일)
moh pouladin
moh pouladin 2018년 8월 26일
댓글: Walter Roberson 2018년 8월 27일
Consider the following function,f:
if x=0:
f(x)=0
else:
f(x)=sin(1/x)
how to formulate that as a Matlab M-File function?
thanks?
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2018년 8월 26일
moh - you state that f is a function but perhaps it is an array instead? are you passing in x to this function and expect to get something returned? Is x an integer or real number? Please provide more details.
moh pouladin
moh pouladin 2018년 8월 26일
f is a function and x is a real number.

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

채택된 답변

Image Analyst
Image Analyst 2018년 8월 26일
This works:
function output = f(x)
output = sin(1 ./ x);
% Zero out any values where x is exactly zero.
output(x == 0) = 0;
% Note, this works regardless if x is a scalar or a vector or matrix.
To call it, here is an example:
x = randi(5, 6, 6) - 1
output = f(x)
  댓글 수: 4
moh pouladin
moh pouladin 2018년 8월 27일
the key point in your script that I missed, is:
output(x == 0) = 0;
would you please suggest a book to read more about Matlab.
thanks

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2011a

Community Treasure Hunt

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

Start Hunting!

Translated by