## Basic Matrix Functions

This help page is divided into three different types of routines:

### Matrix Dimensions

size returns a row vector containing the row and column dimensions of a matrix
length returns the maximum of the row and column dimensions, useful for vectors

#### size

The function size(A) returns the row and column dimensions of the matrix A as a row vector. A simple way of remembering that the row dimension comes first is to remember the expression down the stairs and into the crypt, that is, the rows come first (down) followed by the columns (across).

To return just the column or row dimension, use a second argument of either 1 or 2, respectively.

```>> A = [1 2 3; 4 5 6]

A =

1     2     3
4     5     6

>> size( A )

ans =

2     3

>> size( A, 1 )

ans =

2

In order to find the number of elements in the matrix, just call
prod( size( A ) ):

>> prod( size( A ) )

ans =

6

length

The length function returns the maximum of the column or row
dimensions.  It is useful for finding the dimension of a vector
which may be either an n × 1 column vector, or an
1 × n row vector.  For a matrix A, it
is equivalent to max(size(A)).

>> v = [1 2 3 4 5]

v =

1     2     3     4     5

>> length( v )

ans =

5

Sorts, Sums, and Products

Function

sum
the sum of the entries of a matrix

cumsum
the cummulative sums of the entries of a vector

prod
the sum of the entries of a matrix

cumprod
the cummulative products of the entries of a vector

sort
sorts vector or matrix (matrices by columns)

sortrows
sorts a matrix according by rows

cplxpair
sorts reals and complex conjugate pairs by real part

sum

The sum function sums the entries of either a row
or column vector.  For a matrix, it will sum the columns of the
matrix, producing a row vector.  In order to sum the entries of
a matrix A, you may use sum( sum( A ) ).
To specifically sum along columns or rows, you may specify a
second argument 1 or 2, respectively.

>> sum( [1 2 3 4 5] )

ans =

15

>> A = [1 2 3; 4 5 6]

A =

1     2     3
4     5     6

>> sum( A )

ans =

5     7     9

>> sum( A, 2 )

ans =

6
15

>> sum( sum( A ) )

ans =

21

cumsum

The cumsum function returns the cummulative sums
of the entries of either a row or column vector.
For a matrix, it returns the cummulative sums along the columns of the
matrix.
To specify taking the cummulative sums along columns or rows, use
a second argument 1 or 2, respectively.

The size of the resulting matrix is equal to the size of the
input matrix.

>> cumsum( [1 2 3 4 5] )

ans =

1     3     6    10    15

>> A = [1 2 3; 4 5 6]

A =

1     2     3
4     5     6

>> cumsum( A )

ans =

1     2     3
5     7     9

>> cumsum( A, 2 )

ans =

1     3     6
4     9    15

prod

The prod function sums the entries of either a row
or column vector.  For a matrix, it will take the product of the columns of the
matrix, producing a row vector.  In order to take the product of the entries of
a matrix A, you may use prod( prod( A ) ).
To specifically take the product along columns or rows, you may specify a
second argument 1 or 2, respectively.

>> prod( [1 2 3 4 5] )       % 5!

ans =

120

>> A = [1 2 3; 4 5 6]

A =

1     2     3
4     5     6

>> prod( A )

ans =

4    10    18

>> prod( A, 2 )

ans =

6
120

>> prod( prod( A ) )

ans =

720

cumprod

The cumprod function returns the cummulative products
of the entries of either a row or column vector.
For a matrix, it returns the cummulative products along the columns of the
matrix.
To specify taking the cummulative products along columns or rows, use
a second argument 1 or 2, respectively.

The size of the resulting matrix is equal to the size of the
input matrix.

>> cumprod( [1 2 3 4 5] )

ans =

1     2     6    24   120

>> A = [1 2 3; 4 5 6]

A =

1     2     3
4     5     6

>> cumprod( A )

ans =

1     2     3
4    10    18

>> cumprod( A, 2 )

ans =

1     2     6
4    20   120

sort

For vectors, sort sorts the entries from smallest to
largest.  For a matrix, each column is sorted separately.

Complex values are sorted by real part first, then
by magnitude of the imaginary part.  Thus [1 2 2-i 2+2i 3]
is sorted.

To sort in reverse order, do a sort, and then use fliplr or
flipud.

To sort the rows of a matrix A, use sort( A' )'.

>> sort( [1 5 3 4] )

ans =

1     3     4     5

>> A = [1 3 2; 2 1 3]

A =

1     3     2
2     1     3

>> sort( A )

ans =

1     1     2
2     3     3

sort

For a matrix or vector, sortrows orders the rows
of a matrix so that the first column of the matrix is sorted
from smallest to largest.  If the first column has equal
entries, the entries in subsequent columns are used to break
the tie.

In order to sort along a particular column, use a second
integer argument to indicate which column to sort along.

>> A = [2 3 5; 1 5 6; 2 4 7; 3 5 8; 2 1 9]
A =

2     3     5
1     5     6
2     4     7
3     5     8
2     1     9

>> sortrows( A )

ans =

1     5     6
2     1     9
2     3     5
2     4     7
3     5     8

>> sortrows( A, 2 )

ans =

2     1     9
2     3     5
2     4     7
1     5     6
3     5     8

cplxpair

The function cplxpair sorts a list of real and
complex conjugate pairs (numbers of the form a + bi and a - bi)
into purely real numbers and complex conjugate pairs.  The
complex conjugate pairs are then sorted by their real
part and the size of the imaginary part, and these
are followed by all real numbers sorted from smallest to largest.

This function is usually used to sort the roots of a real
valued polynomial or the eigenvalues of a real matrix.

>> eig( rand( 6 ) )

ans =

3.1830
-0.0609 + 0.3121i
-0.0609 - 0.3121i
0.1361
0.7433 + 0.1790i
0.7433 - 0.1790i

>> cplxpair( ans )

ans =

-0.0609 - 0.3121i
-0.0609 + 0.3121i
0.7433 - 0.1790i
0.7433 + 0.1790i
0.1361
3.1830

Basic Linear Algebra Functions

Function

dot
the dot product of two vectors

cross
the cross product of two vectors

norm
the norm of a vector or a square matrix

cond
the condition number of a matrix

eig
the eigenvalues and eigenvectors of a square matrix

inv
the inverse of a square matrix

rank
the rank of a matrix

det
the determinant of a square matrix

trace
the sum of the diagonal elements of a matrix

svd
the singular value decomposition of a matrix

orth
an orthonormal basis for the range of a matrix

dot

The dot function takes the dot product of two vectors.
If the vectors u and v are both column or both
row vectors, then dot(u, v) is equivalent to u'*v
and u*v', respectively.

>> u = [1 3 5], v = [5 2 3]

u =

1     3     5

v =

5     2     3

>> dot( u, v )

ans =

26

>> u*v'

ans =

26

cross

The dot function takes the cross product of two vectors.
The orientation (row or column) is taken from the first argument.

>> u = [1 3 5], v = [5 2 3]

u =

1     3     5

v =

5     2     3

>> cross( u, v )

ans =

-1    22   -13

norm

The norm(v) function takes Euclidean norm of the vector v.
For a matrix A, the norm nA = norm( A ) is the
smallest value such that norm(A*v) <= nA*norm(v) for all
possible vectors.

The Euclidean norm is also known as the 2-norm.  For information about
other norms, see help norm.

>> norm( [1 2 3 5] )

ans =

6.2450

>> norm( [1 2 3; 5 3 5; 3 2 5] )

ans =

10.3929

cond

The function cond(A) returns the condition number of the
matrix A, which is by definition norm(A) * norm(inv(A)).
A large condition number indicate the matrix is nearly singular.

>> cond( [1 2 3; 5 3 5; 3 2 5] )

ans =

14.0635

eig

The function eig(A) returns the eigenvalues of the
matrix A as a column vector.  If the output is assigned
to a row vector of two variables, the first variable is assigned
a matrix, the columns of which are the eigenvectors of A, and
the second variable a diagonal matrix, the entries of which are
the corresponding eigenvalues.

>> A = [1 2 3; 5 3 5; 3 2 5]

A =

1     2     3
5     3     5
3     2     5

>> eig( A )

ans =

9.4987
-1.4006
0.9020

>> [EVec, EVal] = eig( A )

EVec =

-0.3740   -0.7241   -0.2071
-0.7289    0.6777   -0.8109
-0.5734    0.1276    0.5473

EVal =

9.4987         0         0
0   -1.4006         0
0         0    0.9020

Thus, the eigenvalue, eigenvector pairs are:

9.4987
[-0.3740 -0.7289 -0.5734]

-1.4006
[-0.7241 0.6777 0.1276]

0.9020
[-0.2071 -0.8109 0.5473]

inv

The function inv( A ) takes the inverse of the matrix
A if the inverse exists.

>> A = [1 2 5; 3 2 5; 3 2 2]

A =

1     2     5
3     2     5
3     2     2

>> inv( A )

ans =

-0.5000    0.5000    0.0000
0.7500   -1.0833    0.8333
0    0.3333   -0.3333

>> ans * A

ans =

1.0000    0.0000    0.0000
0.0000    1.0000    0.0000
0         0    1.0000

rank

The rank of a matrix A is the number of linearly independent rows
or columns.  The function rank( A ) estimates this value.

>> A = [1 4 1; 2 5 3; 4 7 7; 2 2 4]

A =

1     4     1
2     5     3
4     7     7
2     2     4

>> rank( A )

ans =

2

det

The function det( A ) returns the determinant of the
square matrix A.  The determinant is the change in volume
which occurs under the transformation.

The determinant is the product of the eigenvalues.

>> A = [1 2 3; 5 3 5; 3 2 5]

A =

1     2     3
5     3     5
3     2     5

>> det( A )

ans =

-12

>> prod( eig( A ) )

ans =

-12.0000

trace

The function trace( A ) returns the sum of the diagonal
entries of the matrix A.  It is equivalent to
sum( diag( A ) ).

The trace is the sum of the eigenvalues.

>> A = [1 2 3; 5 3 5; 3 2 5]

A =

1     2     3
5     3     5
3     2     5

>> trace( A )

ans =

9

>> sum( eig( A ) )

ans =

9.0000

svd

The function svd( A ) of a matrix A returns the
singular values of the matrix A.  If the output is assigned
to a row vector of three variables, the variables are assigned
matrices U, S, and V where U and V are unitary and S is a matrix
the diagonal entries of which are the singular values.

>> A = [1 2 3; 5 3 5; 3 2 5]

A =

1     2     3
5     3     5
3     2     5

>> svd( A )

ans =

10.3929
1.5624
0.7390

>> [U, S, V] = svd( A )

U =

-0.3405    0.7239   -0.6000
-0.7331   -0.6040   -0.3126
-0.5887    0.3335    0.7364

S =

10.3929         0         0
0    1.5624         0
0         0    0.7390

V =

-0.5554   -0.8292    0.0624
-0.3904    0.1938   -0.9000
-0.7342    0.5242    0.4314

>> U*S*V'

ans =

1.0000    2.0000    3.0000
5.0000    3.0000    5.0000
3.0000    2.0000    5.0000

orth

The function orth( A ) returns a matrix, the columns of which
are an orthonormal basis for the range of a matrix A.

The rank of the matrix A is the column dimension of the
resulting matrix.

>> A = [1 3 5; 2 4 4; 0 1 3]

A =

1     3     5
2     4     4
0     1     3

>> orth( A )

ans =

-0.6667    0.3333
-0.6667   -0.6667
-0.3333    0.6667

>> rank( A )

ans =

2

```