How to formulate the following conditional function?
1 次查看(过去 30 天)
显示 更早的评论
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
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.
采纳的回答
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)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!