Logical operators in MATLAB

In this article we see what are the logical operators in matlab and how they work as an example. First of all I remind you that MATLAB is a scripting language emulated by a development environment, it is used for numerical calculation purposes. As a reminder, logical operators allow you to form expressions that will tell your program what to do.
Our article is organized as follows.

In this article we see what are the logical operators in matlab and how they work as an example. First of all I remind you that MATLAB is a scripting language emulated by a development environment, it is used for numerical calculation purposes. As a reminder, logical operators allow you to form expressions that will tell your program what to do.
Our article is organized as follows.

Logical operators in matlab

OperatorDescription
( )bracketing
()[]function call, array declaration
~logical negation (logical NOT)
xorexlusive or operator
allif all array elements are nonzero or true
anyDetermine if any array elements are nonzero
false
Logical 0 (false)
* / %multiplication, division, modulo (remainder of division)
+ -addition, subtraction
bitshift( A , k )returns A shifted to the left by k bits
> >= < <=comparisons
== !=equality/difference
&AND binary
islogicalDetermine if input is logical array
|or inclusive binary
&&Logical AND with short-circuiting(logical AND with sequencing)
||Logical OR with short-circuiting (logical OR with sequencing)
logicalConvert numeric values to logicals
trueLogical 1 (true)

Unary logical operators in MATLAB with examples

Logical negation (logical NOT) in MATLAB

It is a Boolean algebra unary logical operator and expresses a “state” in terms of conditions.

Example in MATLAB :

LOGICALVARIABLE = false;

if ~LOGICALVARIABLE
    disp("negation of variable is true")
else
    disp("negation of variable is false")
end

In MATLAB create a new script and run the selection by Editor > Run Selection

Logical operators in matlab - Logical operators in matlab
Type code script in MATLAB
Logical operators in matlab - Logical operators in matlab
Run the code with “Run selection”

Another exemple of logical negation on a matrix in matlab :

MATRIX = eye(5)
disp(~MATRIX)
Logical operators in matlab - Logical operators matrix negation
logical negation on a matrix in matlab

Operator all in MATLAB- determine if all array elements are nonzero or true

Like the title this operator will check among the first array dimension if all array element are non zero and it will return 0 for false or 1 for true.

Example :

SIMPLEARRAY = [2 1 3]
disp(all(SIMPLEARRAY))

SIMPLEARRAY = [1 1 1]
disp(all(SIMPLEARRAY))

SIMPLEARRAY = [0 0 0]
disp(all(SIMPLEARRAY))


MATRIXARRAY = [2 1 3;1 2 3;3 2 1]
disp(all(MATRIXARRAY))

MATRIXARRAY = [2 0 3;1 0 3;3 0 1]
disp(all(MATRIXARRAY))

MATRIXARRAY = [0 1 3;0 0 3;0 2 1]
disp(all(MATRIXARRAY))
Logical operators in matlab - ALL operator
Understand all operator in MATLAB

Operator any in MATLAB- determine if any array elements are nonzero

Like the operator all, any operator will tests along the first array dimension of an array element and determines if any element is a nonzero number. It will return 0 for false and 1 for true.

You can take the code used before for all operator and replace it with “any” with replace all function :

Logical operators in matlab - ANY operator
operator any in MATLAB
SIMPLEARRAY = [2 1 3]
disp(any(SIMPLEARRAY))

SIMPLEARRAY = [1 1 1]
disp(any(SIMPLEARRAY))

SIMPLEARRAY = [0 0 0]
disp(any(SIMPLEARRAY))


MATRIXARRAY = [2 1 3;1 2 3;3 2 1]
disp(any(MATRIXARRAY))

MATRIXARRAY = [2 0 3;1 0 3;3 0 1]
disp(any(MATRIXARRAY))

MATRIXARRAY = [0 1 3;0 0 3;0 2 1]
disp(any(MATRIXARRAY))

The output of this script will be something like :

SIMPLEARRAY =

     2     1     3

   1


SIMPLEARRAY =

     1     1     1

   1


SIMPLEARRAY =

     0     0     0

   0


MATRIXARRAY =

     2     1     3
     1     2     3
     3     2     1

   1   1   1


MATRIXARRAY =

     2     0     3
     1     0     3
     3     0     1

   1   0   1


MATRIXARRAY =

     0     1     3
     0     0     3
     0     2     1

   0   1   1

>> 

Example of using binary operators in MATLAB

I will show you an example of using binary operators xor / & (and) / | (or) / && / ||. Here we are not exhaustive in relation to all the logical operators that can be binary in MATLAB. However, I think it’s a good base here and covers most use cases.

VARA = true;
VARB = false;
x = 11;

disp("AND = " + (VARA & VARB))
disp("AND = " + and(VARA,VARB))
disp("OR = " + (VARA | VARB))
disp("OR = " + or(VARA,VARB))
disp("XOR = " + xor(VARA,VARB))
disp("&& usage = " + ((x > 3) && (x < 10)))
disp("|| usage = " + ((x > 3) || (x < 10)))

Result is :

AND = false
AND = false
OR = true
OR = true
XOR = true
&& usage = false
|| usage = true
>> 
Logical operators in matlab - binary operator
Logical operators in matlab – binary operators example

The Ternary Operators in MATLAB : an example

Matlab does not have a ternary operator by default, however you can create your own logical operator in MATLAB.

In this example we create a ternary operator which performs a test on condition and which returns a value if true in arg2 and if false in arg3.

Here is the example :

VARA = true;
TRUEVALUE = 56;
FALSEVALUE = 54;

x = 11;

ternaryCondition = @(arg1, arg2, arg3)...
    xor([false true], arg1) * [arg2; arg3];


disp(ternaryCondition(~VARA, TRUEVALUE, FALSEVALUE));
disp(ternaryCondition(VARA, TRUEVALUE, FALSEVALUE));
disp(ternaryCondition(VARA & true, TRUEVALUE, FALSEVALUE));

Result will be :

    54

    56

    56

>> 
Logical operators in matlab - TERNARY logical operators in matlab
Ternary logical operator in MATLAS

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée.