3.4: Basic Complex and Matrix Operations
Complex numbers
m-file environments have excellent support for complex numbers. The imaginary unit is denoted by
i
or
(as preferred in Electrical Engineering)
j
.
To create complex variables \begin{align*}z_1 = 7 + j\end{align*}
z1 = 7 + j
and
z2 = 2*exp(j*pi)
Table 2 gives an overview of the basic functions for manipulating complex numbers, where \begin{align*}z\end{align*}
m-file | |
---|---|
\begin{align*}Re(z)\end{align*} |
real(z) |
\begin{align*}Im(z)\end{align*} |
imag(z) |
\begin{align*}mag(z)\end{align*} |
abs(z) |
\begin{align*}\angle(z)\end{align*} |
angle(z) |
\begin{align*}z^*\end{align*} |
conj(z) |
Operations on Matrices
In addition to scalars, m-file environments can operate on matrices. Some common matrix operations are shown in Table 3; in this table,
M
and
N
are matrices.
Operation | m-file |
---|---|
\begin{align*}M N\end{align*} |
M*N |
\begin{align*}M^{-1}\end{align*} |
inv(M) |
\begin{align*}M^T\end{align*} |
M' |
\begin{align*}det(M)\end{align*} |
det(M) |
Some useful facts:
- The functions
length
and
size
are used to find the dimensions of vectors and matrices, respectively.
- Operations can also be performed on each element of a vector or matrix by proceeding the operator by ".", e.g
.*
,
.^
and
./
.
Example 4
Let \begin{align*}A = \begin{pmatrix}1 & 1\\ 1 & 1\end{pmatrix}\end{align*}
A^2
will return \begin{align*}AA = \begin{pmatrix}2 & 2\\ 2 & 2\end{pmatrix}\end{align*}, while
A.^2
will return \begin{align*}\begin{pmatrix}1^2 & 1^2\\ 1^2 & 1^2\end{pmatrix} = \begin{pmatrix}1 & 1\\ 1 & 1\end{pmatrix}\end{align*}.
Example 5
Given a vector
x
,
compute a vector
y
having elements \begin{align*}y (n) = \tfrac{1}{sin(x(n))}\end{align*}. This can be easily be done with the command
y=1./sin(x)
Note that using
/
in place of
./
would result in the (common) error "Matrix dimensions must agree".
Notes/Highlights Having trouble? Report an issue.
Color | Highlighted Text | Notes | |
---|---|---|---|
Please Sign In to create your own Highlights / Notes | |||
Show More |