Programming  Specialized Matrix Functions

MATLAB has a number of functions that create different kinds of matrices. Some create specialized matrices like the Hankel or Vandermonde matrix. The functions shown in the table below create matrices for more general use.

 Function Description `ones` Create a matrix or array of all ones. `zeros` Create a matrix or array of all zeros. `eye` Create a matrix with ones on the diagonal and zeros elsewhere. `accumarray` Distribute elements of an input matrix to specified locations in an output matrix, also allowing for accumulation. `diag` Create a diagonal matrix from a vector. `magic` Create a square matrix with rows, columns, and diagonals that add up to the same number. `rand` Create a matrix or array of uniformly distributed random numbers. `randn` Create a matrix or array of normally distributed random numbers and arrays. `randperm` Create a vector (1-by-n matrix) containing a random permutation of the specified integers.

Most of these functions return matrices of type `double` (double-precision floating point). However, you can easily build basic arrays of any numeric type using the `ones`, `zeros`, and `eye` functions.

To do this, specify the MATLAB class name as the last argument:

• ```A = zeros(4, 6, 'uint32')
A =
0        0        0        0        0        0
0        0        0        0        0        0
0        0        0        0        0        0
0        0        0        0        0        0
```

Examples

Here are some examples of how you can use these functions.

Creating a Magic Square Matrix.   A magic square is a matrix in which the sum of the elements in each column, or each row, or each main diagonal is the same. To create a 5-by-5 magic square matrix, use the `magic` function as shown.

• ```A = magic(5)
A =
17    24     1     8    15
23     5     7    14    16
4     6    13    20    22
10    12    19    21     3
11    18    25     2     9
```

Note that the elements of each row, each column, and each main diagonal add up to the same value: 65.

Creating a Random Matrix.   The `rand` function creates a matrix or array with elements uniformly distributed between zero and one. This example multiplies each element by 20:

• ```A = rand(5) * 20
A =
3.8686   13.9580    9.9310   13.2046   14.5423
13.6445    7.5675   17.9954    6.8394    6.1858
6.0553   17.2002   16.4326    5.7945   16.7699
10.8335   17.0731   12.8982    6.8239   11.3614
3.0175   11.8713   16.3595   10.6816    7.4083
```

Creating a Diagonal Matrix.   Use `diag` to create a diagonal matrix from a vector. You can place the vector along the main diagonal of the matrix, or on a diagonal that is above or below the main one, as shown here. The `-1` input places the vector one row below the main diagonal:

• ```A = [12 62 93 -8 22];

B = diag(A, -1)
B =
0     0     0     0     0     0
12     0     0     0     0     0
0    62     0     0     0     0
0     0    93     0     0     0
0     0     0    -8     0     0
0     0     0     0    22     0
``` Creating and Concatenating Matrices Concatenating Matrices © 1994-2005 The MathWorks, Inc.