Functional IIF function

Another implementation of the "inline if" function, it one takes function handles instead of values.
273 Downloads
Aktualisiert 24. Mai 2013

Lizenz anzeigen

This function implements the ternary "cond ? then : else" operator. I operates on scalars, matrices and cell arrays.

Instead of precalculated values, it takes function handles which are only applied when the condition is true or false, respectively.

The help text follows:

%iif Implements the ternary ?: operator
% out = iif (@condition, @thenF, elseF@, in[, out])
% out = iif (condition, @thenF, elseF@, in[, out])
%
% The result is equivalent to
% condition(in) ? thenF(in) : elseF(in)
% if condition is a function and
% condition ? thenF(in) : elseF(in)
% else, for each element of the IN argument.
%
% The optional argument OUT serves as a template, if the output type is
% different from the input type, e.g. for mapping arrays to cells and vice
% versa. If it is empty, scalars are assumed for input and output of all
% functions and for the IN argument.
%
% Since using this function may be a bit difficult, here are two examples
% where it is used inside an anonymous function:
%
% (For operation on single values:)
% fun = @(x)(iif(isnumeric(x), ...
% @(y)(y==1), ...
% @(y)(false), ...
% x, []))
%
% (and for operating on a cell array:)
% fun = @(x)(iif(cellfun(@isnumeric, x), ...
% @(y)(num2cell([y{:}]==1)), ...
% @(y)({false}), ...
% x))

Zitieren als

Daniel Hornung (2024). Functional IIF function (https://www.mathworks.com/matlabcentral/fileexchange/41930-functional-iif-function), MATLAB Central File Exchange. Abgerufen .

Kompatibilität der MATLAB-Version
Erstellt mit R2011a
Kompatibel mit allen Versionen
Plattform-Kompatibilität
Windows macOS Linux
Kategorien
Mehr zu Function Creation finden Sie in Help Center und MATLAB Answers
Tags Tags hinzufügen
iif

Community Treasure Hunt

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

Start Hunting!
Version Veröffentlicht Versionshinweise
1.0.0.0