21
x = rand(1,5)
x = x(end:-1:1)
To appreciate the usefulness of these features, compare
these MATLAB statements with a C, Fortran, or Java
routine to do the same operation.
5. MATLAB Functions
MATLAB has a wide assortment of built-in functions.
You have already seen some of them, such as
zeros
,
rand
, and
inv
. This section describes the more common
matrix manipulation functions. For a more complete list,
see Chapter 22, or
Help
:
MATLAB
:
Functions
--
Categorical
List
.
5.1 Constructing matrices
Convenient matrix building functions include:
eye
identity matrix
zeros
matrix of zeros
ones
matrix of ones
diag
create or extract diagonals
triu
upper triangular part of a matrix
tril
lower triangular part of a matrix
rand
randomly generated matrix
hilb
Hilbert matrix
magic
magic square
toeplitz
Toeplitz matrix
gallery
a wide range of interesting matrices
The command
rand(n)
creates an
n
-by-
n
matrix with
randomly generated entries distributed uniformly between
0 and 1 while
rand(m,n)
creates an
m
-by-
n
matrix (
m
and
n
are non-negative integers). Try:
22
A = rand(3)
rand('state',0)
resets the random number generator.
zeros(m,n)
produces an
m
-by-
n
matrix of zeros, and
zeros(n)
produces an
n
-by-
n
one. If
A
is a matrix, then
zeros(size(A))
produces a matrix of zeros having the
same size as
A
. If
x
is a vector,
diag(x)
is the diagonal
matrix with
x
down the diagonal; if
A
is a matrix, then
diag(A)
is a vector consisting of the diagonal of
A
. Try:
x = 1:3
diag(x)
diag(A)
diag(diag(A))
Matrices can be built from blocks. Try creating this 5-by-
5 matrix.
B = [A zeros(3,2) ; pi*ones(2,3), eye(2)]
magic(n)
creates an
n
-by-
n
matrix that is a magic
square (rows, columns, and diagonals have common
sum);
hilb(n)
creates the
n
-by-
n
Hilbert matrix, a very
ill-conditioned matrix. Matrices can also be generated
with a
for
loop (see Section 6.1).
triu
and
tril
extract
upper and lower triangular parts of a matrix. Try:
triu(A)
triu(A) == A
The
gallery
function can generate a matrix from any
one of over 60 different matrix classes. Many have
interesting eigenvalue or singular value properties,
provide interesting counter-examples, or are difficult
matrices for various linear algebraic methods. The
Rosser matrix challenges many eigenvalue solvers:
23
A = gallery('rosser')
eig(A)
eigs(A)
The Parter matrix has many singular values close to π:
A = gallery('parter', 6)
svd(A)
The
eig
,
eigs
, and
svd
functions are discussed below.
5.2 Scalar functions
Certain MATLAB functions operate essentially on scalars
but operate entry-wise when applied to a vector or matrix.
Some of the most common such functions are:
abs ceil floor rem sqrt
acos cos log round tan
asin exp log10 sign
atan fix mod sin
The following statements will generate a sine table:
x = (0:0.1:2)'
y = sin(x)
[x y]
Note that because
sin
operates entry-wise, it produces a
vector
y
from the vector
x
.
5.3 Vector functions and data analysis
Other MATLAB functions operate essentially on a vector
(row or column) but act on an
m
-by-
n
matrix (
m
>
2
) in a
column-by-column fashion to produce a row vector
containing the results of their application to each column.
Row-by-row action can be obtained by using the
transpose (
mean(A')'
, for example) or by specifying the
24
dimension along which to operate (
mean(A,2)
, for
example). Most of these functions perform basic
statistical computations (
std
computes the standard
deviation and
prod
computes the product of the elements
in the vector, for example). The primary functions are:
max sum median any sort var
min prod mean all std
The maximum entry in a matrix
A
is given by
max(max(A))
rather than
max(A)
. Try it. The
any
and
all
functions are discussed in Section 6.6.
5.4 Matrix functions
Much of MATLAB’s power comes from its matrix
functions. Here is a partial list of the most common ones:
eig
eigenvalues and eigenvectors
eigs
like
eig
, for large sparse matrices
chol
Cholesky factorization
svd
singular value decomposition
svds
like
svd
, for large sparse matrices
inv
inverse
lu
LU factorization
qr
QR factorization
hess
Hessenberg form
schur
Schur decomposition
rref
reduced row echelon form
expm
matrix exponential
sqrtm
matrix square root
poly
characteristic polynomial
det
determinant
size
size of an array
length
length of a vector
25
norm
1-norm, 2-norm, Frobenius-norm, ∞-norm
normest
2-norm estimate
cond
condition number in the 2-norm
condest
condition number estimate
rank
rank
kron
Kronecker tensor product
find
find indices of nonzero entries
linsolve
solve a special linear system
MATLAB functions may have single or multiple output
arguments. Square brackets are used to the left of the
equal sign to list the outputs. For example,
y = eig(A)
produces a column vector containing the eigenvalues of
A
, whereas:
[V, D] = eig(A)
produces a matrix
V
whose columns are the eigenvectors
of
A
and a diagonal matrix
D
with the eigenvalues of
A
on
its diagonal. Try it. The matrix
A*V-V*D
will have small
entries.
5.5 The linsolve function
The matrix divide operators (
\
or
/
) are usually enough
for solving linear systems. They look at the matrix and
try to pick the best method. The
linsolve
function acts
like
\
, except that you can tell it about your matrix. Try:
A = [1 2 ; 3 4]
b = [4 10]'
A\b
linsolve(A,b)