0% found this document useful (0 votes)
8 views8 pages

MATLAB Operators Overview and Types

The document provides an overview of operators in MATLAB, including arithmetic, relational, logical, bitwise, and set operations. It explains how these operators function on both scalar and non-scalar data, detailing specific operators and their usage. The document also includes tables summarizing the various operators and their descriptions.

Uploaded by

abdo142377m
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

MATLAB Operators Overview and Types

The document provides an overview of operators in MATLAB, including arithmetic, relational, logical, bitwise, and set operations. It explains how these operators function on both scalar and non-scalar data, detailing specific operators and their usage. The document also includes tables summarizing the various operators and their descriptions.

Uploaded by

abdo142377m
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

8/21/2021 MATLAB - Operators

MATLAB - Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. MATLAB is designed to operate primarily on whole matrices and arrays.
Therefore, operators in MATLAB work both on scalar and non-scalar data. MATLAB allows the
following types of elementary operations −

Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operations
Set Operations

Arithmetic Operators

MATLAB allows two different types of arithmetic operations −


Matrix arithmetic operations
Array arithmetic operations
Matrix arithmetic operations are same as defined in linear algebra. Array operations are
executed element by element, both on one-dimensional and multidimensional array.
The matrix operators and array operators are differentiated by the period (.) symbol. However,
as the addition and subtraction operation is same for matrices and arrays, the operator is same
for both cases. The following table gives brief description of the operators −
Show Examples

[Link] 1/8
8/21/2021 MATLAB - Operators

[Link]. Operator & Description

1 +
Addition or unary plus. A+B adds the values stored in variables A and B. A and B
must have the same size, unless one is a scalar. A scalar can be added to a matrix
of any size.

2 -

Subtraction or unary minus. A-B subtracts the value of B from A. A and B must have
the same size, unless one is a scalar. A scalar can be subtracted from a matrix of
any size.

3 *
Matrix multiplication. C = A*B is the linear algebraic product of the matrices A and B.
More precisely,

For non-scalar A and B, the number of columns of A must be equal to the number of
rows of B. A scalar can multiply a matrix of any size.

4
.*
Array multiplication. A.*B is the element-by-element product of the arrays A and B. A
and B must have the same size, unless one of them is a scalar.

5 /
Slash or matrix right division. B/A is roughly the same as B*inv(A). More precisely,
B/A = (A'\B')'.

6 ./
Array right division. A./B is the matrix with elements A(i,j)/B(i,j). A and B must have
the same size, unless one of them is a scalar.

7 \

Backslash or matrix left division. If A is a square matrix, A\B is roughly the same as
inv(A)*B, except it is computed in a different way. If A is an n-by-n matrix and B is a
column vector with n components, or a matrix with several such columns, then X =
A\B is the solution to the equation AX = B. A warning message is displayed if A is
badly scaled or nearly singular.

[Link] 2/8
8/21/2021 MATLAB - Operators

8
.\
Array left division. A.\B is the matrix with elements B(i,j)/A(i,j). A and B must have
the same size, unless one of them is a scalar.

9 ^
Matrix power. X^p is X to the power p, if p is a scalar. If p is an integer, the power is
computed by repeated squaring. If the integer is negative, X is inverted first. For
other values of p, the calculation involves eigenvalues and eigenvectors, such that if
[V,D] = eig(X), then X^p = V*D.^p/V.

10 .^
Array power. A.^B is the matrix with elements A(i,j) to the B(i,j) power. A and B must
have the same size, unless one of them is a scalar.

11
'
Matrix transpose. A' is the linear algebraic transpose of A. For complex matrices,
this is the complex conjugate transpose.

12 .'
Array transpose. A.' is the array transpose of A. For complex matrices, this does not
involve conjugation.

Relational Operators

Relational operators can also work on both scalar and non-scalar data. Relational operators for
arrays perform element-by-element comparisons between two arrays and return a logical array
of the same size, with elements set to logical 1 (true) where the relation is true and elements set
to logical 0 (false) where it is not.

The following table shows the relational operators available in MATLAB −


Show Examples

[Link] 3/8
8/21/2021 MATLAB - Operators

[Link]. Operator & Description

1
<

Less than

2
<=

Less than or equal to

3 >

Greater than

4 >=

Greater than or equal to

5 ==

Equal to

6 ~=

Not equal to

Logical Operators

MATLAB offers two types of logical operators and functions −

Element-wise − These operators operate on corresponding elements of logical arrays.


Short-circuit − These operators operate on scalar and, logical expressions.
Element-wise logical operators operate element-by-element on logical arrays. The symbols &, |,
and ~ are the logical array operators AND, OR, and NOT.
Short-circuit logical operators allow short-circuiting on logical operations. The symbols && and ||
are the logical short-circuit operators AND and OR.
Show Examples

Bitwise Operations

Bitwise operators work on bits and perform bit-by-bit operation. The truth tables for &, |, and ^
are as follows −

[Link] 4/8
8/21/2021 MATLAB - Operators

p q p&q p|q p^q

0 0 0 0 0

0 1 0 1 1

1 1 1 1 0

1 0 0 1 1

Assume if A = 60; and B = 13; Now in binary format they will be as follows −
A = 0011 1100
B = 0000 1101

-----------------
A&B = 0000 1100
A|B = 0011 1101

A^B = 0011 0001


~A = 1100 0011
MATLAB provides various functions for bit-wise operations like 'bitwise and', 'bitwise or' and
'bitwise not' operations, shift operation, etc.
The following table shows the commonly used bitwise operations −

Show Examples

Function Purpose

bitand(a, b) Bit-wise AND of integers a and b

bitcmp(a) Bit-wise complement of a

bitget(a,pos) Get bit at specified position pos, in the integer array a

bitor(a, b) Bit-wise OR of integers a and b

bitset(a, pos) Set bit at specific location pos of a

bitshift(a, k) Returns a shifted to the left by k bits, equivalent to multiplying by 2k.


Negative values of k correspond to shifting bits right or dividing by 2|k|
and rounding to the nearest integer towards negative infinite. Any
overflow bits are truncated.

bitxor(a, b) Bit-wise XOR of integers a and b

swapbytes Swap byte ordering

[Link] 5/8
8/21/2021 MATLAB - Operators

Set Operations
MATLAB provides various functions for set operations, like union, intersection and testing for set
membership, etc.

The following table shows some commonly used set operations −


Show Examples

[Link] 6/8
8/21/2021 MATLAB - Operators

[Link]. Function & Description

1 intersect(A,B)
Set intersection of two arrays; returns the values common to both A and B. The
values returned are in sorted order.

2 intersect(A,B,'rows')
Treats each row of A and each row of B as single entities and returns the rows
common to both A and B. The rows of the returned matrix are in sorted order.

3 ismember(A,B)
Returns an array the same size as A, containing 1 (true) where the elements of A
are found in B. Elsewhere, it returns 0 (false).

4 ismember(A,B,'rows')
Treats each row of A and each row of B as single entities and returns a vector
containing 1 (true) where the rows of matrix A are also rows of B. Elsewhere, it
returns 0 (false).

5 issorted(A)
Returns logical 1 (true) if the elements of A are in sorted order and logical 0 (false)
otherwise. Input A can be a vector or an N-by-1 or 1-by-N cell array of strings. A is
considered to be sorted if A and the output of sort(A) are equal.

6 issorted(A, 'rows')

Returns logical 1 (true) if the rows of two-dimensional matrix A is in sorted order, and
logical 0 (false) otherwise. Matrix A is considered to be sorted if A and the output
of sortrows(A) are equal.

7
setdiff(A,B)
Sets difference of two arrays; returns the values in A that are not in B. The values in
the returned array are in sorted order.

8
setdiff(A,B,'rows')
Treats each row of A and each row of B as single entities and returns the rows from
A that are not in B. The rows of the returned matrix are in sorted order.
The 'rows' option does not support cell arrays.

[Link] 7/8
8/21/2021 MATLAB - Operators

9 setxor

Sets exclusive OR of two arrays

10 union
Sets union of two arrays

11 unique

Unique values in array

[Link] 8/8

Common questions

Powered by AI

In MATLAB, matrix arithmetic operations are conducted according to the principles of linear algebra, while array arithmetic operations are executed element-by-element. Matrix operations like matrix multiplication require that the number of columns in the first matrix matches the number of rows in the second, whereas array operations such as multiplication (denoted by .*), division (denoted by ./ or .\), and power (denoted by .^) treat each element independently, and matrices must be of the same size unless one is a scalar .

Bitwise operators in MATLAB perform operations on the binary representations of integers using operations such as AND, OR, and XOR. The bitshift operation moves bits to the left or right; shifting to the left by k bits is equivalent to multiplying by 2^k, and shifting to the right by k bits corresponds to division by 2^k. Overflow bits, which cannot be accommodated within the size of the output integer, are truncated and lost during the shift process. The bit representation after the shift, therefore, only retains the bits within the integer's allowed size .

The slash (/) operator, or matrix right division, is used to compute B/A which is similar to B multiplied by the inverse of A (B*inv(A)), and is calculated as (A'\B')'. The backslash (\) operator, or matrix left division, is used to solve equations of the form AX = B, where A must be a square matrix. Here, A\B is effectively similar to computing inv(A)*B, but is solved using different computations that are generally more efficient and robust against numerical errors. The operator issues a warning if A is singular or nearly singular .

Element-wise logical operators operate on arrays by comparing corresponding elements, using symbols & for AND, | for OR, and ~ for NOT. For example, A & B performs a logical AND operation on each corresponding element of matrices A and B. Short-circuit logical operators work on logical expressions involving scalar values or conditions and are represented by && for AND and || for OR, stopping evaluation as soon as the outcome is determined. For example, if (x > 1) && (y < 3) checks whether x is greater than 1 and y is less than 3, and short-circuits if x > 1 is false .

MATLAB's set operations such as intersect and setdiff return logical arrays that represent the intersection and difference of two input arrays, respectively. The result of these operations is automatically sorted in ascending order. Functions like intersect(A, B) return elements common to both arrays, while setdiff(A, B) returns elements found only in A. Additionally, these operations offer a 'rows' option that treats each row of input matrices as a single entity, ensuring sorting in the same row-wise order .

MATLAB's set functions, such as unique and union, ensure uniqueness by removing duplicates and automatically sorting elements in ascending order. This ordering guarantees that the output maintains a consistent structure, crucial for repetitive operations and comparisons with other datasets. When handling matrices, this ensures that row or column operations remain conceptually straightforward, preserving both dimensional integrity and data accuracy, which is essential in applications like machine learning and data analysis .

You would prefer using matrix transpose (A') over array transpose (A.') when dealing with complex matrices if you need the conjugate transpose, which is often required in linear algebraic solutions. The matrix transpose involves conjugating complex numbers, making it suitable for operations like solving Hermitian matrix equations. In contrast, the array transpose does not involve conjugation, which may be preferable when a simple reorientation of matrix dimensions is needed without altering the data's imaginary components .

MATLAB issues a warning related to matrix singularity when performing operations such as matrix division or inversion if it detects that a matrix is nearly singular or badly scaled. This occurs when the determinant is near zero, which can lead to numerical instability and inaccurate results. Such conditions impact computations as the inversion process becomes unreliable, and the results may suffer from round-off errors. It is crucial in these scenarios to check for matrix conditioning, possibly employing regularization techniques or alternatives like pseudo-inversion .

For non-integer exponents, MATLAB computes the matrix power using eigenvalue decomposition, specifically by finding eigenvectors and eigenvalues of the matrix. The matrix is expressed as [V,D] = eig(X), and then X^p is computed as V*D.^p/V, where D is the diagonal matrix of eigenvalues. This method is suited for non-integer powers because it accounts for the matrix's structural properties through eigen-decomposition, allowing consistent operations with complex numbers and providing a stable numerical solution .

Relational operators in MATLAB perform element-by-element comparisons between corresponding elements of matrices and return a logical array of the same size as the input matrices. The logical array contains logical 1 (true) where the relation is satisfied and logical 0 (false) otherwise. This allows for operations such as finding elements greater than, less than, or equal to others across entire matrices simultaneously .

You might also like