The table below summarizes the MATLAB syntax for some basic operations in linear algebra. In what follows, A and B represent matrices while x and y represent column vectors. Additionally, for multiplication operations, the matrices and vectors are assumed to be conformable (i.e., the inner dimensions of the operands must be equal).
Times font (italic for matrices and bold for vectors)
is used to indicate the symbolic definition of an operation
while bold courier font is used to indicate
the equivalent MATLAB operations on workspace variables.
Matrix Operation | Symbolic version | MATLAB syntax |
---|---|---|
Addition, subrtraction | A + B, A - B | A + B, A - B |
Transpose^{+} | A^{T} | A' |
Matrix product | AB | A*B |
Vector-matrix product | Ax | A*x |
Inner (dot, scalar) product | x^{T}y | x'*y, dot(x,y) |
Outer product | xy^{T} | x*y' |
Cross product^{*} | cross(x,y) | |
Matrix inverse | A^{-1} | inv(A), / , \ |
Matrix exponentiation^{**} | A^{n} | A^n |
Determinant | det(A) | det(A) |
Condition number | cond(A) | cond(A), rcond(A) |
Matrix rank | rank(A) | rank(A) |
^{+} If A has complex elements, A' includes a conjugation of the elements in addition to the transpose. This is sometimes referred to as the Hermitian transpose and written as A^{H}.
^{*} This operation is only defined for N = 3.
^{**} A must be a square (N-by-N) matrix and
n must be a scalar.
There are also a number of special and utility matrices that are available:
zeros(m,n) | Creates an m-by-n array of zeros |
ones(m,n) | Creates an m-by-n array of ones |
eye(m,n) | Creates the m-by-n identity matrix (ones on the diagonal) |
rand(m,n) | Creates an m-by-n array of random numbers drawn from the uniform distribution on the interval [0,1] |
randn(m,n) | Creates an m-by-n array of random numbers drawn from the normal distribution with mean 0 and variance 1 (N(0,1)) |
diag(v) | Creates a matrix with the values of the vector v on the diagonal |
See also the help entries on the topics elmat,
specmat, ops and matfun (e.g.,
type the command, help elmat) to see the complete set of
functions and matrices that are available.
There is a class of operations on arrays that make use of "standard"
algebraic rules. These so-called array operations are sometimes called
element-by-element operations because they apply the rules of algebra
to the corresponding elements in two arrays. In what follows, it will be
assumed that the arrays involved in the operation are always of the same
size because array operations are not defined for arrays of dissimilar
sizes. Array operations are commonly used in signal and image processing
and can be valuable in vectorizing MATLAB programs.
Array Operation | Symbolic version | Component version | MATALB equivalent |
---|---|---|---|
Addition, subtraction | A + B, A - B | a_{jk} + b_{jk} , a_{jk} - b_{jk} | A + B, A - B |
Multiplication | a_{ij}b_{ij} | A .* B | |
Division | A÷B | a_{ij} / b_{ij} | A ./ B, B .\ A |
Exponentiation | A^{B} | (a_{ij})^{bij} | A .^ B |
Transpose^{+} | A^{°} | a_{ji} | A .' |
^{+}The only difference between the array and matrix transpose is that the array transpose does not include a conjugation for complex elements.
In essence, an array operation uses as many implied for-loops as needed to apply the scalar operation to the corresponding elements in the arrays that are the arguments of the operation (what a mouthful!). For example, for two dimensional arrays, array multiplication
C = A .* B;
is the same as the following nested loops
[M,N] = size(A) % = size(B), as well! for i = 1:M for j = 1:N C(i,j) = A(i,j)*B(i,j); end end
Exception: The only time when the arrays do not have to be the same size is when one or the other is a scalar (1-by-1) array. In this case, the scalar is automatically promoted (extended) to an array of size suitable to produce a correct result.
There are other array operations that involve rotations of the array about
various "axes" of the array: rot90, flipud and
fliplr. Check out the on-line help for them.
Two cases are possible for solving the equations represented by Ax = b:
Solve for the N-by-1 vector x given that A is an N-by-N matrix and b is an N-by-1 vector. The formal solution to this problem proceeds by finding the inverse (A^{-1}) and forming the solution x = A^{-1}b
In MATLAB notation, the formal method uses the function inv:
x = inv(A)*b (not recommended)
The recommended computational approach for solution in this case is Gauss elimination with pivoting. In MATLAB, the \ operator automatically takes this approach.
x = A\b (recommended: read as "A under b")
The "back-slash" operator is the recommended method because the method it
implements requires fewer operations and is a bit more flexible than
inv. More specifically, the \ operator
involves the L-U decomposition of the (square) matrix (A = LU)
but does not explicitly return those factors. If you want those factors for
further computations or analysis, use the MATLAB function lu.
Solve for the N-by-1 vector x in the equation Ax = b given that A is an M-by-N array and b is an M-by-1 vector.
Since there are more equations than unknowns, the system is over-determined and there is no unique solution. The usual solution strategy in this case is to find the solution x_{LS} that minimizes the difference (Ax - b) using a least-squares criterion. Linear regression (for example, polynomial fitting and general curve fitting) is an important example of this type of problem.
It may be shown (which means there is a good deal of work involved) that the
least-squares solution (x_{LS}) to the problem can be found by
solving the associated "normal" equations,
for x_{LS}. Defining D = A^{T}A and c = A^{T}b shows that the least-squares solution boils down to a solution of Dx_{LS} = c, which is handled by Case 1, given above.
In MATLAB notation, the formal method again makes use to the built-in function inv:
x = inv(A'*A)*(A'*b) (not recommended)
The recommended computational method involves the Q-R decomposition of the matrix A and is implemented by the back-slash operator (\):
x = A\b (recommended)
Note that the recommended solution in this case uses the same syntax as was used for Case 1, above. The \ operator is endowed with the "intelligence" to sense an over-determined set of equations and so will automatically seek the least-squares solution.
The normal equations are not explicitly formed when the \ operator is used. Rather, the \ operator implements the Q-R decomposition of the (rectangular) matrix (A = QR) but does not return those factors. If you want those factors for further use or analysis, use the MATLAB function qr.