Getting Started with MATLAB   Search    Help Desk 

More About Matrices and Arrays

This sections shows you more about working with matrices and arrays, focusing on

Linear Algebra

Informally, the terms matrix and array are often used interchangeably. More precisely, a matrix is a two-dimensional numeric array that represents a linear transformation. The mathematical operations defined on matrices are the subject of linear algebra.

Dürer's magic square

provides several examples that give a taste of MATLAB matrix operations. You've already seen the matrix transpose, A'. Adding a matrix to its transpose produces a symmetric matrix.

The multiplication symbol, *, denotes the matrix multiplication involving inner products between rows and columns. Multiplying a matrix by its transpose also produces a symmetric matrix.

The determinant of this particular matrix happens to be zero, indicating that the matrix is singular.

The reduced row echelon form of A is not the identity.

Since the matrix is singular, it does not have an inverse. If you try to compute the inverse with

you will get a warning message

Roundoff error has prevented the matrix inversion algorithm from detecting exact singularity. But the value of rcond, which stands for reciprocal condition estimate, is on the order of eps, the floating-point relative precision, so the computed inverse is unlikely to be of much use.

The eigenvalues of the magic square are interesting.

One of the eigenvalues is zero, which is another consequence of singularity. The largest eigenvalue is 34, the magic sum. That's because the vector of all ones is an eigenvector.

When a magic square is scaled by its magic sum,

the result is a doubly stochastic matrix whose row and column sums are all one.

Such matrices represent the transition probabilities in a Markov process. Repeated powers of the matrix represent repeated steps of the process. For our example, the fifth power

is

This shows that as k approaches infinity, all the elements in the kth power, Pk, approach 1/4.

Finally, the coefficients in the characteristic polynomial

are

This indicates that the characteristic polynomial

det( A - I )

is

4 - 343 - 642 + 2176

The constant term is zero, because the matrix is singular, and the coefficient of the cubic term is -34, because the matrix is magic!

Arrays

When they are taken away from the world of linear algebra, matrices become two dimensional numeric arrays. Arithmetic operations on arrays are done element-by-element. This means that addition and subtraction are the same for arrays and matrices, but that multiplicative operations are different. MATLAB uses a dot, or decimal point, as part of the notation for multiplicative array operations.

The list of operators includes:

+   

Addition

-   

Subtraction

.*

Element-by-element multiplication

./

Element-by-element division

.\

Element-by-element left division

.^

Element-by-element power

.'  

Unconjugated array transpose

If the Dürer magic square is multiplied by itself with array multiplication

the result is an array containing the squares of the integers from 1 to 16, in an unusual order.

Array operations are useful for building tables. Suppose n is the column vector

Then

builds a table of squares and powers of two.

The elementary math functions operate on arrays element by element. So

builds a table of logarithms.

Multivariate Data

MATLAB uses column-oriented analysis for multivariate statistical data. Each column in a data set represents a variable and each row an observation. The (i,j)th element is the ith observation of the jth variable.

As an example, consider a data set with three variables:

For five observations, the resulting array might look like:

The first row contains the heart rate, weight, and exercise hours for patient 1, the second row contains the data for patient 2, and so on. Now you can apply many of MATLAB's data analysis functions to this data set. For example, to obtain the mean and standard deviation of each column:

For a list of the data analysis functions available in MATLAB, type

If you have access to the Statistics Toolbox, type

Scalar Expansion

Matrices and scalars can be combined in several different ways. For example, a scalar is subtracted from a matrix by subtracting it from each element. The average value of the elements in our magic square is 8.5, so

forms a matrix whose column sums are zero.

With scalar expansion, MATLAB assigns a specified scalar to all indices in a range. For example:

zeros out a portion of B

Logical Subscripting

The logical vectors created from logical and relational operations can be used to reference subarrays. Suppose X is an ordinary matrix and L is a matrix of the same size that is the result of some logical operation. Then X(L) specifies the elements of X where the elements of L are nonzero.

This kind of subscripting can be done in one step by specifying the logical operation as the subscripting expression. Suppose you have the following set of data.

The NaN is a marker for a missing observation, such as a failure to respond to an item on a questionnaire. To remove the missing data with logical indexing, use finite(x), which is true for all finite numerical values and false for NaN and Inf.

Now there is one observation, 5.1, which seems to be very different from the others. It is an outlier. The following statement removes outliers, in this case those elements more than three standard deviations from the mean.

For another example, highlight the location of the prime numbers in Dürer's magic square by using logical indexing and scalar expansion to set the nonprimes to 0.

The find Function

The find function determines the indices of array elements that meet a given logical condition. In its simplest form, find returns a column vector of indices. Transpose that vector to obtain a row vector of indices. For example

picks out the locations, using one-dimensional indexing, of the primes in the magic square.

Display those primes, as a row vector in the order determined by k, with

When you use k as a left-hand-side index in an assignment statement, the matrix structure is preserved.



[ Previous | Help Desk | Next ]