Next  |  Prev  |  Top  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search

Elementary operations

MATLAB works with essentially only one kind of object, a rectangular matrix with complex elements. If the imaginary parts of the elements are all zero, they are not printed, but they still occupy storage. In some situations, special meaning is attached to 1 by 1 matrices, that is scalars, and to 1 by n and m by 1 matrices, that is row and column vectors.

Matrices can be introduced into MATLAB in four different ways:

The explicit list is surrounded by angle brackets, '$<$' and '$>$', and uses the semicolon ';' to indicate the ends of the rows. For example, the input line

   A = <1 2 3; 4 5 6; 7 8 9>
will result in the output
   A     =

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

The matrix A will be saved for later use. The individual elements are separated by commas or blanks and can be any MATLAB expressions, for example

   x = < -1.3, 4/5, 4*atan(1) >
results in
   X     =

     -1.3000   0.8000   3.1416

The elementary functions available include sqrt, log, exp, sin, cos, atan, abs, round, real, imag, and conjg.

Large matrices can be spread across several input lines, with the carriage returns replacing the semicolons. The above matrix could also have been produced by

   A = < 1 2 3
         4 5 6
         7 8 9 >

Matrices can be input from the local file system. Say a file named 'xyz' contains five lines of text,

   A = <
   1 2 3
   4 5 6
   7 8 9
   >;
then the MATLAB statement EXEC('xyz') reads the matrix and assigns it to A .

The FOR statement allows the generation of matrices whose elements are given by simple formulas. Our example matrix A could also have been produced by

   for i = 1:3, for j = 1:3, a(i,j) = 3*(i-1)+j;
The semicolon at the end of the line suppresses the printing, which in this case would have been nine versions of A with changing elements.

Several statements may be given on a line, separated by semicolons or commas.

Two consecutive periods anywhere on a line indicate continuation. The periods and any following characters are deleted, then another line is input and concatenated onto the previous line.

Two consecutive slashes anywhere on a line cause the remainder of the line to be ignored. This is useful for inserting comments.

Names of variables are formed by a letter, followed by any number of letters and digits, but only the first 4 characters are remembered.

The special character prime (') is used to denote the transpose of a matrix, so

   x = x'
changes the row vector above into the column vector
   X     =

     -1.3000
      0.8000
      3.1416

Individual matrix elements may be referenced by enclosing their subscripts in parentheses. When any element is changed, the entire matrix is reprinted. For example, using the above matrix,

   a(3,3) = a(1,3) + a(3,1)
results in
   A     =

       1.    2.    3.
       4.    5.    6.
       7.    8.   10.

Addition, subtraction and multiplication of matrices are denoted by +, -, and * . The operations are performed whenever the matrices have the proper dimensions. For example, with the above A and x, the expressions A + x and x*A are incorrect because A is 3 by 3 and x is now 3 by 1. However,

   b = A*x
is correct and results in the output
   B     =

      9.7248
     17.6496
     28.7159
Note that both upper and lower case letters are allowed for input (on those systems which have both), but that lower case is converted to upper case.

There are two "matrix division" symbols in MATLAB,
and / . (If your terminal does not have a backslash, use $ instead, or see CHAR.) If A and B are matrices, then A\B and B/A correspond formally to left and right multiplication of B by the inverse of A , that is inv(A)*B and B*inv(A), but the result is obtained directly without the computation of the inverse. In the scalar case, 3
1 and 1/3 have the same value, namely one-third. In general, A\B denotes the solution X to the equation A*X = B and B/A denotes the solution to X*A = B.

Left division, A\B, is defined whenever B has as many rows as A . If A is square, it is factored using Gaussian elimination. The factors are used to solve the equations A*X(:,j) = B(:,j) where B(:,j) denotes the j-th column of B. The result is a matrix X with the same dimensions as B. If A is nearly singular (according to the LINPACK condition estimator, RCOND), a warning message is printed. If A is not square, it is factored using Householder orthogonalization with column pivoting. The factors are used to solve the under- or overdetermined equations in a least squares sense. The result is an m by n matrix X where m is the number of columns of A and n is the number of columns of B . Each column of X has at most k

nonzero components, where k is the effective rank of A .

Right division, B/A, can be defined in terms of left division by B/A = (A'\B')'.

For example, since our vector b was computed as A*x, the statement

   y = A\b
results in
   Y     =

     -1.3000
      0.8000
      3.1416
Of course, y is not exactly equal to x because of the roundoff errors involved in both A*x and A, but we are not printing enough digits to see the difference. The result of the statement
   e = x - y
depends upon the particular computer being used. In one case it produces
   E     =

      1.0e-15 *

        .3053
       -.2498
        .0000
The quantity 1.0e-15 is a scale factor which multiplies all the components which follow. Thus our vectors x and y actually agree to about 15 decimal places on this computer.

It is also possible to obtain element-by-element multiplicative operations. If A and B have the same dimensions, then A .* B denotes the matrix whose elements are simply the products of the individual elements of A and B . The expressions A ./ B and A .
B give the quotients of the individual elements.

There are several possible output formats. The statement

   long, x
results in
   X     =

      -1.300000000000000
        .800000000000000
       3.141592653589793
The statement
   short
restores the original format.

The expression A**p means A to the p-th power. It is defined if A is a square matrix and p is a scalar. If p is an integer greater than one, the power is computed by repeated multiplication. For other values of p the calculation involves the eigenvalues and eigenvectors of A.

Previously defined matrices and matrix expressions can be used inside brackets to generate larger matrices, for example

   C = <A, b; <4 2 0>*x, x'>
results in
   C     =

      1.0000   2.0000   3.0000   9.7248
      4.0000   5.0000   6.0000  17.6496
      7.0000   8.0000  10.0000  28.7159
     -3.6000  -1.3000   0.8000   3.1416

There are four predefined variables, EPS, FLOP, RAND and EYE. The variable EPS is used as a tolerance is determining such things as near singularity and rank. Its initial value is the distance from 1.0 to the next largest floating point number on the particular computer being used. The user may reset this to any other value, including zero. EPS is changed by CHOP, which is described in §11.

The value of RAND is a random variable, with a choice of a uniform or a normal distribution.

The name EYE is used in place of I to denote identity matrices because I is often used as a subscript or as sqrt(-1). The dimensions of EYE are determined by context. For example,

   B = A + 3*EYE
adds 3 to the diagonal elements of A and
   X = EYE/A
is one of several ways in MATLAB to invert a matrix.

FLOP provides a count of the number of floating point operations, or "flops", required for each calculation.

A statement may consist of an expression alone, in which case a variable named ANS is created and the result stored in ANS for possible future use. Thus

   A\A - EYE
is the same as
   ANS = A\A - EYE
(Roundoff error usually causes this result to be a matrix of "small" numbers, rather than all zeros.)

All computations are done using either single or double precision real arithmetic, whichever is appropriate for the particular computer. There is no mixed-precision arithmetic. The Fortran COMPLEX data type is not used because many systems create unnecessary underflows and overflows with complex operations and because some systems do not allow double precision complex arithmetic.

2. MATLAB functions

Much of MATLAB's computational power comes from the various matrix functions available. The current list includes:

   INV(A)          - Inverse.
   DET(A)          - Determinant.
   COND(A)         - Condition number.
   RCOND(A)        - A measure of nearness to singularity.
   EIG(A)          - Eigenvalues and eigenvectors.
   SCHUR(A)        - Schur triangular form.
   HESS(A)         - Hessenberg or tridiagonal form.
   POLY(A)         - Characteristic polynomial.
   SVD(A)          - Singular value decomposition.
   PINV(A,eps)     - Pseudoinverse with optional tolerance.
   RANK(A,eps)     - Matrix rank with optional tolerance.
   LU(A)           - Factors from Gaussian elimination.
   CHOL(A)         - Factor from Cholesky factorization.
   QR(A)           - Factors from Householder orthogonalization.
   RREF(A)         - Reduced row echelon form.
   ORTH(A)         - Orthogonal vectors spanning range of A.
   EXP(A)          - e to the A.
   LOG(A)          - Natural logarithm.
   SQRT(A)         - Square root.
   SIN(A)          - Trigonometric sine.
   COS(A)          - Cosine.
   ATAN(A)         - Arctangent.
   ROUND(A)        - Round the elements to nearest integers.
   ABS(A)          - Absolute value of the elements.
   REAL(A)         - Real parts of the elements.
   IMAG(A)         - Imaginary parts of the elements.
   CONJG(A)        - Complex conjugate.
   SUM(A)          - Sum of the elements.
   PROD(A)         - Product of the elements.
   DIAG(A)         - Extract or create diagonal matrices.
   TRIL(A)         - Lower triangular part of A.
   TRIU(A)         - Upper triangular part of A.
   NORM(A,p)       - Norm with p = 1, 2 or 'Infinity'.
   EYE(m,n)        - Portion of identity matrix.
   RAND(m,n)       - Matrix with random elements.
   ONES(m,n)       - Matrix of all ones.
   MAGIC(n)        - Interesting test matrices.
   HILBERT(n)      - Inverse Hilbert matrices.
   ROOTS(C)        - Roots of polynomial with coefficients C.
   DISPLAY(A,p)    - Print base p representation of A.
   KRON(A,B)       - Kronecker tensor product of A and B.
   PLOT(X,Y)       - Plot Y as a function of X .
   RAT(A)          - Find "simple" rational approximation to A.
   USER(A)         - Function defined by external program.
Some of these functions have different interpretations when the argument is a matrix or a vector and some of them have additional optional arguments. Details are given in the HELP document in the appendix.

Several of these functions can be used in a generalized assignment statement with two or three variables on the left hand side. For example

   <X,D> = EIG(A)
stores the eigenvectors of A in the matrix X and a diagonal matrix containing the eigenvalues in the matrix D. The statement
   EIG(A)
simply computes the eigenvalues and stores them in ANS.

Future versions of MATLAB will probably include additional functions, since they can easily be added to the system.


Next  |  Prev  |  Top  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search