After reading this MATLAB operators topic, you will understand its theory and examples also you will able to use various types of MATLAB operators.
An operator is a symbol that defines the kind of operation that compiler has to perform. MATLAB having various types of operators are classified as
- Arithmetic operators
- Relational operators
- Logical operators
MATLAB – Arithmetic operators
For explanation purpose, we will discuss arithmetic operators with considering scalars only. The arithmetic operators with examples are
Operation | Operator | Example |
---|---|---|
Addition | + | 2+3 |
Subtraction | - | 2-3 |
Multiplication | * | 2*3 |
Right division | / | 2/3 |
Left division | \ | 2\3=3/2 |
Exponentiation | ^ | 2^3 |
MATLAB VIEW – to show Arithmetic operations
The example below will show you how to use arithmetic operators in MATLAB.
Examples
Program (1): To perform addition, subtraction, multiplication, right division, left division and exponentiation operations on x and y given as x = 2; y = 3, in MATLAB..
x=2 y=3 add=x+y sub=x-y mul=x*y rdiv=x/y ldiv=x\y expo=x^y
MATLAB VIEW – Program (1):
Create a script file in MATLAB and type the following code –
Output (1):
x = 2 y = 3 add = 5 sub = -1 mul = 6 rdiv = 0.6667 ldiv = 1.5000 expo = 8
MATLAB VIEW – Output (1):
MATLAB – Logical operators:
For explanation purpose, we will discuss Logical operators with considering scalars only. The Logical operators with examples are
Operation | Operator | Example |
---|---|---|
Logical AND | & | A&B |
Logical OR | | | A|B |
Logical NOT | ~ | A~B |
MATLAB VIEW – to show Logical operations
The example below will show you how to use logical operators in MATLAB.
Examples
Program (1): To perform Logical AND and Logical OR operations on x and y are given as x = -2 ; y = -3. Also, perform Logical NOT operation on x only.
x=-2 y=-3 land=x&y lor=x|y lnot=~x
MATLAB VIEW – Program (1):
Create a script file in MATLAB and type the following code –
Output (1):
x = -2 y = -3 land = 1 lor = 1 lnot = 0
MATLAB VIEW – Output (1):
MATLAB – Relational operators:
For explanation purpose, we will discuss relational operators with considering scalars only. The Relational operators with examples are
Operation | Operator |
---|---|
Less than | < |
Less than or equal to | <= |
Greater than | > |
Greater than or equal to | >= |
Not equal to | ~= |
MATLAB VIEW – to show Relational operations
The example below will show you how to use relational operators in MATLAB.
Examples
Program (1): To perform less than, less than or equal to, greater than, greater than or equal to, equal to and not equal to operations on x and y is given as x = – 2; y = – 3, in MATLAB.
x=-2 y=-3 lt=x<y lte=x<=y gt=x>y gte=x>=y et=x==y net=x~=y
MATLAB VIEW – Program (1):
Create a script file in MATLAB and type the following code –
Output (1):
x = -2 y = -3 lt = 0 lte = 0 gt = 1 gte = 1 et = 0 net = 1