if Function
if(cond, x, y)
Returns x if logical condition cond is true (or non-zero),
y otherwise.
cond is usually an expression involving a logical/boolean operator. For example you can use:
for a logical AND
for a logical OR
for an equality
x is the value returned when cond is true.
y is the value returned when cond is false.
Examples
1.
2.
3.
use the vectorize operator to apply the function status to each element of the vector temperature:
4.
Here is a simple function:
The if function can be used to create piecewise functions.
5.
6.
7.
is
when
or
, and
otherwise:
8.
is
when
, is
when
, and 0 otherwise: