The Matlab function `spiral' was mentioned in the description of algo. Another such function is `magic(n)', which returns an nxn `magic square'.
A very different type of function is `rand(n)', which returns an nxn matrix of `random numbers' in the interval from 0 to 1. Unlike `spiral' and `magic', which produce a determined matrix, `rand' produces a random matrix, a different one each time. To find a random matrix of integers to test the techniques of this chapter, give, for example, the command round(100*rand(3)) to find a random 3x3 matrix of two digit integers.
The easiest way to generate matrices that are not square is to generate square matrices and remove rows or columns. For example, you can make a random 5x3 matrix of two digit numbers by entering B = round(100*rand(5)) and then A = B(1:5, 1:3). (The second command defines A as the matrix whose rows are rows 1 through 5 of B and whose columns are columns 1 through 3 of B.)
The Matlab function `diag' makes generating diagonal matrices easy. For example, try entering the command diag([3 2 1 4]) to see what matrix results. (The input to `diag' is a matrix with one row, which explains the square brackets inside the parentheses.) To make a diagonal matrix that is not square--and the conditions under which diagonal matrices are equivalent depend to some extent on whether the matrices are square or not--just create a square diagonal matrix and then omit orws or columns as above.
Return to Chapter 2.