...

/

Arithmetic and Logical Operators

Arithmetic and Logical Operators

Compare the arithmetic and logical operators in MATLAB and Python.

Arithmetic operators

The table below compares the arithmetic operators of MATLAB and Python.

Operators

MATLAB

Python

Addition

+

+

Subtraction

-

-

Multiplication

.* and *

*

Division

.\, ./, / and \

/

Power

.^ and ^

** and pow()

Modulo

mod() and rem()

%

Quotient

fix()

//

Rounding

floor(), ceil(), fix() and round()

round()

Greater than, greater than or equal to

>, >=

>, >=

Less than, less than or equal to

<, <=

<, <=

Equal to

==

==

Not equal to

~=

!=

Due to the large collection of Python libraries, we can implement almost all MATLAB operators in Python. Let’s look at a few examples.

Multiplication, division, and power operators

MATLAB has several operators for performing multiplication, division, and power operations on arrays. The . operator performs the element-wise operations, For example:

Press + to interact
var1 = [2,3;4,5]
var2 = [2,1;3,2]
disp("Multiplication of two matrices (var1 * var2)");
disp(var1*var2);
disp("Multiplication of corresponding elements (element by element var1 * var2)");
disp(var1.*var2);
disp("Division of two matrices (var1 * (var2^-1))");
disp(var1/var2);
disp("Division of corresponding elements (element by element var1/var2)");
disp(var1./var2);
disp("Division of two matrices ((var1^-1)*var2)");
disp(var1\var2);
disp("Division of corresponding elements (element by element var2/var1)");
disp(var1.\var2);
disp("Cube of a matrix (var1 * var1 * var1)");
disp(var1^3);
disp("Element wise power of matrix");
disp(var1.^3);

The table below shows how the above MATLAB operations can be performed in Python using NumPy ...