| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
expr1 & expr2
expr1 | expr2
~expr
The symbols &, |, and ~ are the logical array operators AND, OR, and NOT. These operators are commonly used in conditional statements, such as if and while, to determine whether or not to execute a particular block of code. Logical operations return a logical array with elements set to 1 (true) or 0 (false), as appropriate.
expr1 & expr2 represents a logical AND operation between values, arrays, or expressions expr1 and expr2. In an AND operation, if expr1 is true and expr2 is true, then the AND of those inputs is true. If either expression is false, the result is false. Here is a pseudocode example of AND:
IF (expr1: all required inputs were passed) AND ... (expr2: all inputs are valid) THEN (result: execute the function)
expr1 | expr2 represents a logical OR operation between values, arrays, or expressions expr1 and expr2. In an OR operation, if expr1 is true or expr2 is true, then the OR of those inputs is true. If both expressions are false, the result is false. Here is a pseudocode example of OR:
IF (expr1: S is a string) OR ... (expr2: S is a cell array of strings) THEN (result: parse string S)
~expr represents a logical NOT operation applied to expression expr. In a NOT operation, if expr is false, then the result of the operation is true. If expr is true, the result is false. Here is a pseudocode example of NOT:
IF (expr: function returned a Success status) is NOT true THEN (result: throw an error)
The function xor(A,B) implements the exclusive OR operation.
The expression operands for AND, OR, and NOT are often arrays of nonsingleton dimensions. When this is the case, The MATLAB software performs the logical operation on each element of the arrays. The output is an array that is the same size as the input array or arrays.
If just one operand is an array and the other a scalar, then the scalar is matched against each element of the array. When the operands include two or more nonscalar arrays, the sizes of those arrays must be equal.
This table shows the output of AND, OR, and NOT statements that use scalar and/or array inputs. In the table, S is a scalar array, A is a nonscalar array, and R is the resulting array:
| Operation | Result |
|---|---|
| S1 & S2 | R = S1 & S2 |
| S & A | R(1) = S & A(1); ... R(2) = S & A(2); ... |
| A1 & A2 | R(1) = A1(1) & A2(1); R(2) = A1(2) & A2(2); ... |
| S1 | S2 | R = S1 | S2 |
| S | A | R(1) = S | A(1); R(2) = S | A(2); ... |
| A1 | A2 | R(1) = A1(1) | A2(1); R(2) = A1(2) | A2(2); ... |
| ~S | R = ~S |
| ~A | R(1) = ~A(1); R(2) = ~A(2), ... |
The number of expressions that you can evaluate with AND or OR is not limited to two (e.g., A & B). Statements such as the following are also valid:
expr1 & expr2 & expr3 | expr4 & expr5
Use parentheses to establish the order in which MATLAB evaluates a compound operation. Note the difference in the following two statements:
(expr1 & expr2) | (expr3 & expr4) % 2-component OR expr1 & (expr2 | expr3) & expr4 % 3-component AND
The precedence for the logical operators with respect to each other is shown in the table below. MATLAB always gives the & operator precedence over the | operator. Although MATLAB typically evaluates expressions from left to right, the expression a|b&c is evaluated as a|(b&c). It is a good idea to use parentheses to explicitly specify the intended precedence of statements containing combinations of & and |.
Operator | Operation | Priority |
|---|---|---|
NOT | Highest | |
Elementwise AND | ||
Elementwise OR | ||
Short-circuit AND | ||
Short-circuit OR | Lowest |
The &, and |operators do not short-circuit. See the documentation on the && and || operators if you need short-circuiting capability.
When used in the context of an if or while expression, and only in this context, the elementwise & and | operators use short-circuiting in evaluating their expressions. That is, A&B and A|B ignore the second operand, B, if the first operand, A, is sufficient to determine the result.
So, although the statement 1|[] evaluates to false, the same statement evaluates to true when used in either an if or while expression:
A = 1; B = []; if(A|B) disp 'The statement is true', end; The statement is true
while the reverse logical expression, which does not short-circuit, evaluates to false:
if(B|A) disp 'The statement is true', end;
Another example of short-circuiting with elementwise operators shows that a logical expression such as the one shown below, which under most circumstances is invalid due to a size mismatch between A and B, works within the context of an if or while expression:
The A|B statement generates an error:
A = [1 1]; B = [2 0 1]; A|B ??? Error using ==> or Matrix dimensions must agree.
But the same statement used to test an if condition does not error:
if (A|B) disp 'The statement is true', end; The statement is true
The following is a truth table for the operators and functions in the previous example.
| Inputs | and | or | not | xor | |
|---|---|---|---|---|---|
| A | B | A & B | A | B | ~A | xor(A,B) |
| 0 | 0 | 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 | 0 | 0 |
These logical operators have M-file function equivalents, as shown here.
Logical Operation | Equivalent Function |
|---|---|
| A & B | and(A,B) |
| A | B | |
| ~A |
Using OR in a conditional statement, call function parseString on S, but only if S is a character array or a cell array of strings:
if ischar(S) || iscellstr(S)
parseString(S)
endFind those elements of array R that are both greater than 0.3 AND less then 0.9:
rand('state',0);
R=rand(5,7);
R>0.3 & R<0.9
ans =
0 1 1 1 0 0 0
0 1 1 0 1 0 1
1 0 0 0 1 1 1
1 1 1 1 0 0 0
1 1 0 1 0 0 1Find those elements of array R that are greater than or equal to 25 AND are less than or equal to 50:
rand('state',0);
R = rand(3,5) * 50;
R > 40
ans =
1 0 0 0 1
0 1 0 0 0
0 0 1 0 0
Throw an error if the return status of a function does NOT indicate success:
[Z, status] = myfun(X, Y);
if ~(status == SUCCESS);
error('Error in function myfun')
endThis example shows the logical OR of the elements in the vector u with the corresponding elements in the vector v:
u = [0 0 1 1 0 1]; v = [0 1 1 0 0 1]; u | v ans = 0 1 1 1 0 1
all, any, find, logical, xor, true, false
Logical Operators: Short-circuit && ||
Relational Operators < > <= >= == ~=
![]() | Relational Operators < > <= >= == ~= | Logical Operators: Short-circuit && || | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2010- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |