The symbols & and && perform element-wise and short-circuited AND operations, respectively.

## Preparation

Create two matrices using the following code:

A = [0, 1, 0; 1, 0, 1];
B = [1, 1, 0; 0, 0, 1];

## Element-wise AND

A & B & … performs an element-wise AND of all input matrices A, B, etc.

% Element-wise AND of matrices A and B
R = A & B;
To learn how logical AND works, see Table 1.

All nonscalar input matrices must have the same size. If the logical expression contains both scalar and nonscalar inputs, the scalars are treated as if they were the same size as the other inputs.

## Short-circuited AND

expr1 && expr2 performs a short-circuited AND. Both expr1 and expr2 must evaluate to a scalar logical result. It is called “short-circuited” because expr2 is not evaluated if expr1 is false.

% Short-circuited AND
i = 8;
inRange = (i >= 0) && (i <= 7);

Note: A false value in expr1 is enough to consider the entire AND operation as false (Table 1).

## Truth table

A logical AND operation returns true if all operands are true, otherwise returns false (Table 1).

Table 1. Truth table of the logical AND operation.
A B A AND B
True True True
True False False
False True False
False False False

Note: By convention, zero is considered false and nonzero numbers are considered true.

I recommend the following books to learn more on logical operations in MATLAB:

1. This book was the winner of a 2017 Textbook Excellence Award. Chapters 4 and 5 explain selection statements and loops, two areas that extensively make use of logical operations.

2. MATLAB for Engineers (5th Edition)

This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. The eighth chapter covers logical operations and selection statements.

I also recommend the following page in the MATLAB help documentation, and the other tutorials in this series.

doc and;

## Exercise

Consult the help documentation of the functions rand and sum. Use rand to create a matrix X of size 3×4. Calculate how many elements in X are in the interval $$[0.5,0.75]$$.

## Source code

I hope you have learned how to perform a logical AND in MATLAB. The source code developed in this tutorial is available at this page.