Help with conditional statement

1 visualización (últimos 30 días)
Mathew Gacosta
Mathew Gacosta el 13 de Mayo de 2021
Respondida: Star Strider el 13 de Mayo de 2021
How do I program this on Matlab?

Respuestas (1)

Star Strider
Star Strider el 13 de Mayo de 2021
My approach to these problems is to take advantage of MATLAB’s logical vectors.
So for example, if I wanted to construct a function that was +1 for x-values between -1 and 0, and -1 for x-values between 1 and 2, I would do this —
f = @(x) 1.*((x>=-1) & (x<=0)) + (-1).*((x>=1) & (x<=2));
x = linspace(-3, 3);
figure
plot(x, f(x))
grid
ylim([-1 1]*1.1)
You can also use an if block.

Categorías

Más información sobre Programmatic Model Editing 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