&
Element-wise logical AND operator.
Category: Language Fundamentals > Operators and Elementary Operations
Syntax
C = A & B
Returns 1 where both A and B are nonzero. Same as and(A, B).
Description
& combines conditions on whole arrays, for example x(x > 0 & x < 10) selects the entries between 0 and 10. Both operands are always evaluated.
In an if or while condition, prefer &&, which stops as soon as the answer is known and requires scalar operands.
Input arguments
A, B- Logical or numeric arrays with compatible sizes. Any nonzero value counts as true.
Output arguments
C- Logical array of the size given by implicit expansion.
Example
x = [-2 3 8 12];
x > 0 & x < 10
x(x > 0 & x < 10)
[1 0 2] & [1 1 0]
Output
ans = 1×4 logical array
0 1 1 0
ans = 1×2
3 8
ans = 1×3 logical array
1 0 0