Help on Boolean expressions and selection statements (completely copied from MATLAB's help resources) ------------------------------------------------------------------------------- >> help relop Relational operators. < > Relational operators. The six relational operators are <, <=, >, >=, ==, and ~=. A < B does element by element comparisons between A and B and returns a matrix of the same size with elements set to one where the relation is true and elements set to zero where it is not. A and B must have the same dimensions (or one can be a scalar). & Logical AND. A & B is a matrix whose elements are 1's where both A and B have non-zero elements, and 0's where either has a zero element. A and B must have the same dimensions (or one can be a scalar). | Logical OR. A | B is a matrix whose elements are 1's where either A or B has a non-zero element, and 0's where both have zero elements. A and B must have the same dimensions (or one can be a scalar). ~ Logical complement (NOT). ~A is a matrix whose elements are 1's where A has zero elements, and 0's where A has non-zero elements. xor Exclusive OR. xor(A,B) is 1 where either A or B, but not both, is non-zero. See XOR. ------------------------------------------------------------------------------- >> help if IF IF statement condition. The general form of the IF statement is IF expression statements ELSEIF expression statements ELSE statements END The statements are executed if the real part of the expression has all non-zero elements. The ELSE and ELSEIF parts are optional. Zero or more ELSEIF parts can be used as well as nested IF's. The expression is usually of the form expr rop expr where rop is ==, <, >, <=, >=, or ~=. Example if I == J A(I,J) = 2; elseif abs(I-J) == 1 A(I,J) = -1; else A(I,J) = 0; end See also RELOP, ELSE, ELSEIF, END, FOR, WHILE, SWITCH. ------------------------------------------------------------------------------- Getting Started: Programming with MATLAB: if The if statement evaluates a logical expression and executes a group of statements when the expression is true. The optional elseif and else keywords provide for the execution of alternate groups of statements. An end keyword, which matches the if, terminates the last group of statements. The groups of statements are delineated by the four keywords - no braces or brackets are involved. MATLAB's algorithm for generating a magic square of order n involves three different cases: when n is odd, when n is even but not divisible by 4, or when n is divisible by 4. This is described by if rem(n,2) ~= 0 M = odd_magic(n) elseif rem(n,4) ~= 0 M = single_even_magic(n) else M = double_even_magic(n) end In this example, the three cases are mutually exclusive, but if they weren't, the first true condition would be executed. It is important to understand how relational operators and if statements work with matrices. When you want to check for equality between two variables, you might use if A == B, ... This is legal MATLAB code, and does what you expect when A and B are scalars. But when A and B are matrices, A == B does not test if they are equal, it tests where they are equal; the result is another matrix of 0's and 1's showing element-by-element equality. In fact, if A and B are not the same size, then A == B is an error. The proper way to check for equality between two variables is to use the isequal function, if isequal(A,B), ... Here is another example to emphasize this point. If A and B are scalars, the following program will never reach the unexpected situation. But for most pairs of matrices, including our magic squares with interchanged columns, none of the matrix conditions A > B, A < B or A == B is true for all elements and so the else clause is executed. if A > B 'greater' elseif A < B 'less' elseif A == B 'equal' else error('Unexpected situation') end Several functions are helpful for reducing the results of matrix comparisons to scalar conditions for use with if, including isequal isempty all any ------------------------------------------------------------------------------- Programming and Data Types: M-File Programming: if, else, and elseif if evaluates a logical expression and executes a group of statements based on the value of the expression. In its simplest form, its syntax is if logical_expression statements end If the logical expression is true (1), MATLAB executes all the statements between the if and end lines. It resumes execution at the line following the end statement. If the condition is false (0), MATLAB skips all the statements between the if and end lines, and resumes execution at the line following the end statement. For example, if rem(a,2) == 0 disp('a is even') b = a/2; end You can nest any number of if statements. If the logical expression evaluates to a nonscalar value, all the elements of the argument must be nonzero. For example, assume X is a matrix. Then the statement if X statements end is equivalent to if all(X(:)) statements end The else and elseif statements further conditionalize the if statement: The else statement has no logical condition. The statements associated with it execute if the preceding if (and possibly elseif condition) is false (0). The elseif statement has a logical condition that it evaluates if the preceding if (and possibly elseif condition) is false (0). The statements associated with it execute if its logical condition is true (1). You can have multiple elseifs within an if block. if n < 0 % If n negative, display error message. disp('Input must be positive'); elseif rem(n,2) == 0 % If n positive and even, divide by 2. A = n/2; else A = (n+1)/2; % If n positive and odd, increment and divide. end if Statements and Empty Arrays An if condition that reduces to an empty array represents a false condition. That is, if A S1 else S0 end will execute statement S0 when A is an empty array. ------------------------------------------------------------------------------- MATLAB Function Reference: if if: Conditionally execute statements Syntax if expression statements end if expression1 statements elseif expression2 statements else statements end Description if conditionally executes statements. The simple form is: if expression statements end More complicated forms use else or elseif. Each if must be paired with a matching end. Arguments expression A MATLAB expression, usually consisting of smaller expressions or variables joined by relational operators (==, <, >, <=, >=, or ~=). Two examples are: count < limit and (height - offset) >= 0. Expressions may also include logical functions, as in: isreal(A). Simple expressions can be combined by logical operators (&,|,~) into compound expressions such as: (count < limit) & ((height - offset) >= 0). statements One or more MATLAB statements to be executed only if the expression is true (or nonzero). See Examples for information about how nonscalar variables are evaluated. Examples Here is an example showing if, else, and elseif: for i = 1:n for j = 1:n if i == j a(i,j) = 2; elseif abs([i j]) == 1 a(i,j) = 1; else a(i,j) = 0; end end end Such expressions are evaluated as false unless every element-wise comparison evaluates as true. Thus, given matrices A and B: A = B = 1 0 1 1 2 3 3 4 The expression: A < B Evaluates as false since A(1,1) is not less than B(1,1). A < (B+1) Evaluates as true since no element of A is greater than the corresponding element of B. A & B Evaluates as false since A(1,2) | B(1,2) is false. 5 > B Evaluates as true since every element of B is less than 5. See Also break, else, end, for, return, switch, while ------------------------------------------------------------------------------- MATLAB Function Reference: elseif elseif Conditionally execute statements Syntax if expression statements elseif expression statements end Description The elseif command conditionally executes statements. if expression statements elseif expression statements end The second block of statements executes if the first expression has any zero elements and the second expression has all nonzero elements. The expression is usually the result of expression rop expression where rop is ==, <, >, <=, >=, or ~=. else if, with a space between the else and the if, differs from elseif, with no space. The former introduces a new, nested, if, which must have a matching end. The latter is used in a linear sequence of conditional statements with only one terminating end. The two segments if A if A x = a x = a else elseif B if B x = b x = b elseif C else x = c if C else x = c x = d else end x = d end end end produce identical results. Exactly one of the four assignments to x is executed, depending upon the values of the three logical expressions, A, B, and C. See Also break, else, end, for, if, return, switch, while ------------------------------------------------------------------------------- MATLAB Function Reference: else else Conditionally execute statements Syntax if expression statements else statements end Description The else command is used to delineate an alternate block of statements. if expression statements else statements end The second set of statements is executed if the expression has any zero elements. The expression is usually the result of expression rop expression where rop is ==, <, >, <=, >=, or ~=. See Also break, elseif, end, for, if, return, switch, while -------------------------------------------------------------------------------