'if then else' dalam anonymous function


Contoh fungsi:

$f(x) =

  \begin{cases}

    \displaystyle \frac{x}{c}       & \quad \text{for } 0 \leq x < c\\

    \displaystyle \frac{1-x}{1-c}       & \quad \text{for } c \leq x < 1\\

  \end{cases}

$

Contoh penulisan di Octave:

close all
clear
clc

inline_if = @(varargin) varargin{2*find([varargin{1:2:end}],1,'first')};
f = @(c,x) inline_if(0 <= x & x < c, x/c, c <= x && x <= 1, (1-x)/(1-c));

x = linspace(0,1,10000);
y = zeros(size(x));

for i = 1:length(x)
  y(i) = f(0.3,x(i));
end

figure
plot(x,y,'LineWidth',1.5)
axis square