next up previous contents
Next: Flow Up: The Basics Previous: Building Matrices

Matrix and Element-wise Operations

Some operations are intended for matrices in particular. These include the conjugate and non-conjugate transpose operators ' and .', the matrix multiplication operator tex2html_wrap_inline2423 , and the left and right matrix ``division'' operators tex2html_wrap_inline2561 and /. For instance, if A is a matrix and x and b are vectors, then the lines

.1ex>> A'
.1ex>> A2
.1ex>> A * x = b
.1ex>> x = A tex2html_wrap_inline2561 b
.1ex>> x * A = b
.1ex>> x = A/b

respectively take the conjugate transpose of A, take the square of A, give a typical matrix equation involving matrix multiplication, give the solution for that equation, give another matrix equation, and give the solution for the second equation.

Such solutions to matrix equations are solved exactly (with Gaussian elimination) if the matrix is square; others are solved in a least-squares sense (with Householder orthogonalization). Also see help slash.

To get inner and outer products of vectors, remember their formal definitions. The inner product is given by x' * y = y' * x, and the outer products by x * y' and y * x' = (x * y')'.

MATLAB understands multiplication and division between a matrix and a scalar in the normal sense;

.1ex>> 10 * [1 2; 3 4]
ans =

If you want to take two matrices (or vectors) and multiply or divide them element by element, or if you want to exponentiate each element of a matrix, place a period before the operator. These are array operations as opposed to matrix operations.

.1ex>> [1 2; 3 4].2
ans =

Exponentiation also has both matrix and array forms. If x and y are scalars and A and B are matrices, y tex2html_wrap_inline2591 x, A tex2html_wrap_inline2591 x, and x tex2html_wrap_inline2591 A have their usual mathematical meanings. Array exponentiation is available with A. tex2html_wrap_inline2591 x to raise each element to a power, and A. tex2html_wrap_inline2591 B to raise each element of A to the power of the corresponding element of B.

MATLAB also has a large number of matrix functions to implement common mathematical operations, such as finding eigenvalues and eigenvectors. For instance, kron will give the (Kronecker) tensor product. See help matfun.

If you apply a function that operates on scalars to a matrix or vector, or if you apply a function that operates on vectors to a matrix, MATLAB performs the operation element-wise. Scalar functions will be applied to each element of the matrix, and the result will be a matrix of the same size. Vector functions will be applied to each column of the matrix, and the result will be a row vector of the same width. (Use the transpose operators to effect row-by-row application.) See help funm if you want to use the matrix and not the array version of a function. Lastly, functions defined strictly on the real line are applied separately to the real and imaginary parts of a complex number.

.1ex>> sin([0 (pi/6) (pi/2) pi])
ans =

.1ex>> max([1 10; 20 2])
ans =

.1ex>> max(max([1 10; 20 2]))
ans =

.1ex>> round(1.7+3.2i)
ans =
+ 3i Certain functions are particularly useful for this. The functions max, min, median, mean, std, sum, and prod take a vector and return its maximum, minimum, median, arithmetic mean, standard deviation, element sum, and element product (respectively). Applied to a matrix, they return a row vector of the result on each column. sort sorts a vector (or each column of a matrix) in ascending order. Also see help datafun.

Applying operations element-wise is a powerful feature of MATLAB and using it is the fastest and best way to accomplish most things.


next up previous contents
Next: Flow Up: The Basics Previous: Building Matrices

sepherke
Sat Mar 21 21:42:28 EST 1998