Borrar filtros
Borrar filtros

How to formulate the following conditional function?

2 visualizaciones (últimos 30 días)
moh pouladin
moh pouladin el 26 de Ag. de 2018
Comentada: Walter Roberson el 27 de Ag. de 2018
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 comentarios
Geoff Hayes
Geoff Hayes el 26 de Ag. de 2018
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 el 26 de Ag. de 2018
f is a function and x is a real number.

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 26 de Ag. de 2018
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 comentarios
moh pouladin
moh pouladin el 27 de Ag. de 2018
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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2011a

Community Treasure Hunt

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

Start Hunting!

Translated by