Create a matrix using the following code:
A = [0, 1, 0; 1, 0, 1];
A = 0 1 0 1 0 1
~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;
R = 1 0 1 0 1 0
% Check if the matrix A has elements hasElements = ~isempty(A);
hasElements = 1
A logical NOT operation returns the logical complement of its unique operand (Table 1).
Note: By convention, zero is considered
false and nonzero numbers are considered
I recommend the following books to learn more on logical operations in MATLAB:
This book was the winner of a Textbook Excellence Award. Chapters 4 and 5 explain selection statements and loops, two areas that extensively make use of logical operations.
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.
Consult the help documentation of the functions
sum. Calculate how many elements in an arbitrary matrix
X are multiple of 2.
A possible solution could be:
modResult = mod(X,2); count = sum(~modResult(:));
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.