Table of Contents
Introduction
MATLAB has many built in functions that create matrices with special patterns or properties. These special matrices are widely used in numerical methods, simulations, testing, and examples. In this chapter you will see how to create and work with some of the most common ones, and how they are useful when you are experimenting with vectors and matrices.
You do not need to know advanced linear algebra to follow this chapter. The focus is on what the functions do and how to use them in MATLAB.
The `zeros`, `ones`, and `eye` Functions
Some of the simplest and most commonly used special matrices are filled with zeros, filled with ones, or form an identity matrix. MATLAB provides the functions zeros, ones, and eye for this purpose.
The identity matrix is very important in linear algebra. An identity matrix of size $n \times n$ has ones on the main diagonal and zeros everywhere else. In MATLAB you create it with eye.
For example, to create a $3 \times 3$ zero matrix, a $2 \times 4$ ones matrix, and a $4 \times 4$ identity matrix, you can write:
A = zeros(3, 3);
B = ones(2, 4);
C = eye(4);You can also use these functions to create vectors. If you give a single size argument, MATLAB creates a square matrix. If you give two size arguments, the first is the number of rows and the second is the number of columns.
It is common to create an array of zeros or ones to act as an initial value before filling it with your own data. For example, you might preallocate a vector of zeros and then fill it in a loop, or create an identity matrix to represent a simple linear transformation.
Special Constant Matrices with `diag` and `eye`
The main diagonal of a matrix is the set of elements $a_{11}, a_{22}, a_{33}$, and so on. MATLAB lets you create diagonal matrices easily. A diagonal matrix has nonzero elements only on its main diagonal and zeros everywhere else.
The function diag can turn a vector into a diagonal matrix. For example:
d = [1 2 3 4];
D = diag(d);
The matrix D will be a $4 \times 4$ matrix with the elements of d on the diagonal and zeros elsewhere.
You can also use diag in the opposite way. If you pass a matrix, MATLAB returns a column vector containing the diagonal elements:
mainDiag = diag(D);
The function eye can be combined with scalar multiplication to form simple diagonal matrices. For example, $5 I$ for a $3 \times 3$ identity matrix is:
I3 = eye(3);
M = 5 * I3;This creates a $3 \times 3$ matrix whose diagonal entries are all $5$ and whose off diagonal entries are zero.
The `rand`, `randn`, and `randi` Functions
Random matrices are very useful for testing functions and algorithms. MATLAB provides several functions to create matrices filled with random values.
The function rand generates uniformly distributed random numbers in the interval $(0, 1)$. For example, a $2 \times 3$ random matrix is:
R = rand(2, 3);
The function randn creates random numbers with a normal (Gaussian) distribution with mean $0$ and variance $1$. This is useful if you want numbers that are usually close to zero but can be positive or negative:
G = randn(4, 4);
The function randi creates random integers in a specified range. You specify the maximum or both minimum and maximum. For example:
R1 = randi(10, 3, 3); % integers from 1 to 10
R2 = randi([-5, 5], 2, 4); % integers from -5 to 5
Each time you call these functions, MATLAB generates new random values. If you want to reproduce the same random numbers, you need to control the random number generator, which is done with rng. You will usually use rng when you want repeatable results for testing or teaching.
Patterned Matrices with `magic`, `pascal`, and `hilb`
MATLAB includes functions that create special square matrices that follow mathematical patterns. These are often used as standard examples in linear algebra and numerical analysis.
A magic square is a square matrix of distinct integers where the sums of each row, each column, and both main diagonals are the same. MATLAB creates these with magic. For example:
M = magic(3);
The matrix M will be a $3 \times 3$ magic square. You can verify its property by summing rows and columns with the sum function.
Another special matrix is the Pascal matrix. It contains binomial coefficients, which are familiar from the Pascal triangle. You can create a Pascal matrix with pascal:
P = pascal(4);
The entries of P follow a precise mathematical rule involving combinations. You do not need to know the formula to use the matrix, but it is useful for certain numerical experiments.
The Hilbert matrix is another classic test matrix. Its entries are given by
$$
H_{ij} = \frac{1}{i + j - 1}
$$
for row index $i$ and column index $j$. This means that the matrix elements are all fractions. In MATLAB, you create a Hilbert matrix with hilb:
H = hilb(5);The Hilbert matrix is known to be numerically difficult to work with, which makes it a good example for testing numerical stability. You will mostly see it in numerical linear algebra contexts.
Triangular and Toeplitz Matrices
A triangular matrix is a square matrix in which all entries above or below the main diagonal are zero. MATLAB can create triangular matrices using the tril and triu functions. They extract the lower or upper triangular part of a matrix.
For example, given a matrix A, you can write:
L = tril(A); % lower triangular part
U = triu(A); % upper triangular part
By default, tril includes the main diagonal and everything below it, and triu includes the main diagonal and everything above it. You can pass an optional second argument to control which diagonals are kept.
The toeplitz function creates a Toeplitz matrix, which is constant along each diagonal. That is, all elements with the same difference j - i share the same value. To create a Toeplitz matrix, you typically specify the first column and optionally the first row:
c = [1 2 3 4];
r = [1 5 6 7];
T1 = toeplitz(c); % symmetric Toeplitz matrix
T2 = toeplitz(c, r); % general Toeplitz matrix
In T1, the first column also defines the first row, so the matrix is symmetric. In T2, MATLAB uses c as the first column and r as the first row, and then fills the diagonals accordingly.
Toeplitz matrices appear often in signal processing and time series, where values depend on time differences rather than absolute time, but at this stage you can think of them as another example of a structured matrix.
Special Test Matrices with `gallery`
MATLAB includes a function called gallery that can create a large collection of special matrices, mostly for testing and demonstrating numerical algorithms. The matrices created by gallery have many different structures and properties.
The basic usage is:
A = gallery('name', n);
Here 'name' is a string that chooses the type of matrix, and n is usually the size. For example:
K = gallery('kahan', 5);
W = gallery('wilkinson', 7);
Each gallery matrix has specific mathematical properties. For example, some are used to test eigenvalue routines, others to test solvers. As a beginner, you do not need to memorize the details of each type. Instead, you can use gallery when you want interesting example matrices without having to define them yourself.
You can see which types are available and get descriptions by looking at the documentation for gallery in MATLAB. This is a good way to explore advanced test matrices when you begin to learn more linear algebra and numerical methods.
Constructing Special Matrices from Basic Functions
The basic building blocks zeros, ones, eye, and diag are often enough to build your own simple structured matrices.
For example, to create a matrix that has ones on the main diagonal and twos right above it, you can write:
n = 4;
A = eye(n) + 2 * diag(ones(1, n-1), 1);
The second argument to diag here, 1, places the vector on the first superdiagonal, which is the diagonal immediately above the main one. If you used -1 instead, it would go one diagonal below the main one.
As another example, to create a band matrix that has ones on the main diagonal and ones on both the first upper and first lower diagonals, you might write:
n = 5;
main = ones(1, n);
upper = ones(1, n-1);
lower = ones(1, n-1);
B = diag(main) + diag(upper, 1) + diag(lower, -1);These constructions show how you can combine simple special matrices to form more complex patterns that are tailored to your particular problem.
Summary of Important Points
Special matrices are created with dedicated functions such as zeros, ones, eye, diag, rand, randn, randi, magic, pascal, hilb, tril, triu, toeplitz, and gallery.
Use zeros, ones, and eye to create basic constant matrices and to preallocate arrays.
Use diag to build or extract diagonal matrices or diagonals from existing matrices.
Use rand, randn, and randi to generate test data with random values.
Use patterned matrices like magic, pascal, hilb, triangular matrices, and Toeplitz matrices when you need structured examples or when following numerical linear algebra demonstrations.
You can combine these basic functions to construct your own custom structured matrices.