Tutorial

Logical NOT in MATLAB

The symbol ~ performs a logical NOT in MATLAB.

Preparation

Create a matrix using the following code:

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

Syntax

~A performs a logical NOT of the input matrix A. In most cases, you will use ~ to directly negate the result of a logical expression.

% Logical NOT of the matrix A
R = ~A;
% Check if the matrix A has elements
hasElements = ~isempty(A);
To learn how logical NOT works, see Table 1.

Truth table

A logical NOT operation returns the logical complement of its unique operand (Table 1).

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

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

Further reading

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

  1. MATLAB: A Practical Introduction to Programming and Problem Solving (4th Edition)

    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 not;

Exercise

Consult the help documentation of the functions mod and sum. Calculate how many elements in an arbitrary matrix X are multiple of 2.

Source code

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

Share

Your friends may need this knowledge to complete a task. Share this tutorial.