MATLAB Operator
An operator is a symbol that tells the compiler to perform various numerical or logical manipulations. MATLAB is designed to operate mainly on whole matrices and arrays. Therefore, functions in MATLAB work both on scalar and non-scalar data.
MATLAB has several types of operators, symbols, and special characters to deal with variables, functions, and arithmetic operations.
MATLAB Arithmetic Operators
Arithmetic operators help in performing simple arithmetic operations like addition, subtraction, multiplication, division, and power.
Symbol | Role | Corresponding function |
---|---|---|
+ | Addition | plus |
+ | Unary plus | uplus |
– | Subtraction | minus |
– | Unary minus | uminus |
.* | Element-wise multiplication | times |
* | Matrix multiplication | mtimes |
./ | Element-wise right division | rdivide |
. | Element-wise left division | ldivide |
/ | Matrix right division | mrdivide |
Matrix left division | mldivide | |
.^ | Element-wise power | power |
^ | Matrix power | mpower |
.’ | Transpose | transpose |
‘ | Complex conjugate transpose | ctranspose |
Arithmetic Operators and Arrays
Except for some matrix operators, MATLAB arithmetic operators work on corresponding functions of arrays with equal dimensions. For vectors and rectangular array, both operands must be the equivalent size unless one is a scalar. If one operand is a scalar and the other is not, MATLAB applies the scalar to every item of the other operand, this property is called scalar expansion.
This example uses scalar expansion to evaluate the product of a scalar operand and a matrix.
MATLAB Relational Operators
Relational operators perform value comparison operations.
Symbol | Role | Corresponding function |
---|---|---|
== | Equal to | eq |
~= | Not equal to | ne |
> | Greater than | gt |
>= | Greater than or equal to | ge |
< | Less thanlt | |
<= | Less than or equal to | le |
Relational Operators and Arrays
The MATLAB relational operators compare corresponding components of arrays with equal dimensions. Relational operators always operate element-by-element. In this example, the resulting matrix present where the element of A is equal to the corresponding part of B.
For vectors and rectangular array, both operands must be the same size unless one is a scalar. In this case, where one operand is a scalar, and the other is not, MATLAB tests the scalar against every element of the other operand. Locations where the particular relation is true receive logical 1. Locations where the relation is false receive logical 0.
MATLAB Logical Operators
Logical operators perform logical operations and output the result in Boolean state true or false using the numbers 1 and 0, respectively.
MATLAB offer three types of logical operators and functions:
- Element-wise: It works on corresponding elements of logical arrays.
- Bit-wise: It works on corresponding bits of integer values or arrays.
- Short-circuit: It works on scalar, logical expressions.
The values returned by MATLAB logical operators and functions, with the exception of bit-wise functions, are of type logical and are suitable for use with logical indexing.
Element-Wise Operators and Functions
The following logical operators and functions execute element-wise logical operations on their inputs to produce a like-sized output array. The examples are shown in the following table use vector inputs A and B, where
A = [0 1 1 0 1];
B = [1 1 0 0 1];
Symbol | Role | Description | Example |
---|---|---|---|
& | Logical ANDIt returns 1 for every element location that is true (nonzero) in both arrays and 0 for all other elements. | A & B = 01001 | |
| | Logical ORIt returns 1 for every element location that is true (nonzero) in either one or the other, or both arrays, and 0 for all other elements. | A | B = 11101 | |
~ | Logical NOTIt complements each element of the input array, A. | ~A = 10010 | |
xor | It returns 1 for every element location that is true (nonzero) in only one array, and 0 for all other elements. | xor(A,B)=10100 |
For operators and functions that take two array operands (&, |, and xor), both arrays must have the same dimensions, with each dimension being the same size. The one exception to this is where one operand is a scalar, and the other is not.
Note: MATLAB converts any finite nonzero, mathematic values used as inputs to logical expressions to logical 1, or true.
Bit-Wise Operator
The following functions execute bit-wise logical operations on nonnegative integer inputs. Inputs may be scalar or in arrays. If in arrays, these operations produce a like-sized output array.
The examples are present in the following table use scalar inputs A and B, where
A = 28; % binary 11100
B = 21; % binary 10101
Function | Description | Example |
---|---|---|
bitand | It returns the bit-wise AND of two nonnegative integer arguments. | bitand(A,B) = 20 (binary 10100) |
bitor | It returns the bit-wise OR of two nonnegative integer arguments. | bitor(A,B) = 29 (binary 11101) |
bitcmp | It returns the bit-wise complement as an n-bit number, where n is the second input argument to bitcmp. | bitcmp(A,5) = 3 (binary 00011) |
bitxor | It returns the bit-wise exclusive OR of two nonnegative integer arguments. | bitxor(A,B) = 9 (binary 01001) |
Short-Circuit Operators
The following operators execute AND and OR operations on logical expressions, including scalar values. They are short-circuiting operators in that they calculate their second operand only when the first operand does not fully determine the output.
Operator | Description |
---|---|
&& | It returns logical 1 (true) if both inputs calculate to true, and logical 0 (false) if they do not. |
|| | It returns logical 1 (true) if either input, or both, calculate to true, and logical 0 (false) if they do not. |
MATLAB Special Characters
Special characters perform some particular tasks according to their behavior and the position where they are used.
Symbol | Symbol Name | Role |
---|---|---|
@ | At symbol |
|
. | Period or dot |
|
… | Dot dot dot or ellipsis |
|
, | Comma |
|
: | Colon |
|
; | Semicolon |
|
( ) | Parentheses |
|
[ ] | Square brackets |
|
{ } | Curly brackets |
|
% | Percent |
|
%{ %} | Percent curly bracket |
|
! | Exclamation point |
|
? | Question mark |
|
‘ ‘ | Single quotes |
|
” “ | Double quotes |
|
N/A | Space character |
|
~ | Tilde |
|
= | Equal sign |
|
MATLAB String and Character Formatting Special Characters
There are some special characters to use only within the text of a character or string. These special characters are used to insert newlines or carriage returns, specify folder paths.
Symbol | Symbol Name | Role | Example |
---|---|---|---|
/ | Forward-slash Backslash | File or folder path separation | Windows: dir([matlabroot ‘toolboxmatlabelmatscriptview1.m’]) or dir([matlabroot ‘/toolbox/matlab/elmat/scriptview1.m’]) UNIX/Linux system: only forward slash dir([matlabroot ‘/toolbox/matlab/elmat/scriptview1.m’]) |
.. | Dot dot | Parent folder | cd ….example Goes up two levels and then down into the example folder |
* | Asterisk | Wildcard character | dir(‘example_*.mat’) Finds all files with names start with example and have a .mat extension |
@ | At symbol | Class folder indicator | @myScriptClassget.m |
+ | Plus | Package directory indicator | +mypack +mypack/scriptview1.m +mypack/@myScriptClass |