## Summary of Array and Matrix Computations In MATLAB

### Topics covered

[Back to main page] [Back to numerics page]

### Matrix (or Linear) Algebra

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 OperationSymbolic versionMATLAB syntax
Addition, subrtractionA + B, A - B A + B, A - B
Transpose+AT A'
Matrix productAB A*B
Vector-matrix productAx A*x
Inner (dot, scalar) productxTy x'*y, dot(x,y)
Outer productxyT x*y'
Cross product* cross(x,y)
Matrix inverseA-1 inv(A), / , \
Matrix exponentiation** An A^n
Determinantdet(A) det(A)
Condition numbercond(A) cond(A), rcond(A)
Matrix rankrank(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 AH.

* This operation is only defined for N = 3.

** A must be a square (N-by-N) matrix and n must be a scalar.

### Utility Arrays

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.

### Array Operations

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 OperationSymbolic versionComponent version MATALB equivalent
Addition, subtractionA + B, A - B ajk + bjk , ajk - bjk A + B, A - B
Multiplication aijbij A .* B
Division A÷B aij / bij A ./ B, B .\ A
Exponentiation AB (aij)bij A .^ B
Transpose+ A° aji 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.

### Solving linear systems in MATLAB

Two cases are possible for solving the equations represented by Ax = b:

• Case 1: "N equations in N unknowns" problem.

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-1b

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.

• Case 2: "M equations in N unknowns" problem.

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 xLS 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 (xLS) to the problem can be found by solving the associated "normal" equations,

(ATA)xLS = ATb

for xLS. Defining D = ATA and c = ATb shows that the least-squares solution boils down to a solution of DxLS = 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.

[Back to main page] [Back to numerics page]
Comments? Contact Jim Maneval at maneval@bucknell.edu