creating a function with a condition set

9 visualizaciones (últimos 30 días)
Hollie Pietsch
Hollie Pietsch el 16 de Sept. de 2017
Editada: Walter Roberson el 16 de Sept. de 2017
How do I create this function in Matlab? All of the tutorials seem to use very simple syntax in the functions.
f(t) = {
  3 comentarios
Walter Roberson
Walter Roberson el 16 de Sept. de 2017
Perhaps post an image of the formula.
Hollie Pietsch
Hollie Pietsch el 16 de Sept. de 2017

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 16 de Sept. de 2017
Editada: Walter Roberson el 16 de Sept. de 2017
If you have Symbolic Toolbox and R2016b or later, you can use piecewise
Otherwise, you need something like
function ft = f(t)
ft = nan(size(t));
mask = t >= 0 & t < 2;
ft(mask) = t(mask) - 1;
mask = t >= 2 & t < 4;
ft(mask) = 3 - t(mask);
Notice the nan: your function definition does not define any value for t outside of [0, 4) so no number can be assigned for those cases.

Categorías

Más información sobre Vehicle Scenarios en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by