Matrix IIF

Returns first or second expression depending of a condition result
433 download
Aggiornato 30 set 2010

Visualizza la licenza

iif(Cond, ExprA,ExprB) is inspired in a traditional function from Visual Basic that receives a condition and returns ExprA or ExprB, depending of COND evaluation (true or false)

As MatLab is a matrix language, iif was implemented as a matrix function.

Suppose that COND compares a matrix with a number so COND is a matrix of same dimensions. The matrix result contains just A or B elements, depending if COND is true ("A") or false ("B")

Example:
A = [ 1 5 8 3 ];
disp( iff(A > 4, 100,-200) )

The display is
[ -200 100 100 -200 ]

Normally it can be done of a less intuitive way
(the method that iif was internally implemented)
A = (A>4)*100 + (A<=4)*-200

It allows powerful inline matrix operations:

Supose A and B two scores set from a student, the average system is the mean, with the biggest score with weight 2 and the other with weight 1.

A =[7 8 3 4];
B = [6 7 7 9];
disp(iif(A>B,(2*A+B)/3, (2*B+A)/3))

Result is
[6.66 7.66 5.66 7.33]

The alternative way is (and the form that was internally implemented)
(A>B).*(2*A+B)/3 + (A<=B).*(2*B+A)/3

At last, it works if COND is a scalar condition.

For instance

a='Paul';
b='John';
disp(iif(strcmpi(a,b),'equal','different'))

Shows 'different'

Cita come

Paulo Buchsbaum (2026). Matrix IIF (https://it.mathworks.com/matlabcentral/fileexchange/28811-matrix-iif), MATLAB Central File Exchange. Recuperato .

Compatibilità della release di MATLAB
Creato con R2009b
Compatibile con qualsiasi release
Compatibilità della piattaforma
Windows macOS Linux
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e MATLAB Answers
Versione Pubblicato Note della release
1.2.0.0

add new feature for character arrays

1.1.0.0

Just minor correction in the Description text

1.0.0.0