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.

[TOPICS]

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.

[TOPICS]

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.

[TOPICS]

Solving linear systems in MATLAB

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



[TOPICS]

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