1 2 3 4 5 6
function val = clamp(n, m, M) % returns n if n is between m and M, otherwise % m if n < m % M if n > m val = min(max(n, m), M); end