Working with Matrices#

MATLAB is the matrix laboratory and the programming language is designed to work efficiently with matrices. Let’s take a look at how to create matrices and how to perform matrix computations.

See also

Check out the MATLAB documentation to learn more about matrices.

Manual Construction#

The simplest way to construct a matrix is to use square brackets [ ... ] and manually type the entries separated by a space (or comma ,) with rows separated by a semicolon ;. For example, let’s create the matrix

\[\begin{split} A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} \end{split}\]
A = [1 2 3; 4 5 6; 7 8 9]
A =

     1     2     3
     4     5     6
     7     8     9

Construction Functions#

There are functions such as zeros, ones, eye and diag for constructing matrices. For example, create a 2 by 5 matrix of zeros:

zeros(2,5)
ans =

     0     0     0     0     0
     0     0     0     0     0

Create the identity matrix of size 6:

eye(6)
ans =

     1     0     0     0     0     0
     0     1     0     0     0     0
     0     0     1     0     0     0
     0     0     0     1     0     0
     0     0     0     0     1     0
     0     0     0     0     0     1

Create a 3 by 2 matrix of ones:

ones(3,2)
ans =

     1     1
     1     1
     1     1

Create a diagonal matrix:

diag([1 2 3])
ans =

     1     0     0
     0     2     0
     0     0     3

Create a matrix with entries on the upper diagonal:

diag([1 2 3],1)
ans =

     0     1     0     0
     0     0     2     0
     0     0     0     3
     0     0     0     0

Create a matrix with entries on the lower diagonal:

diag([1 2 3],-1)
ans =

     0     0     0     0
     1     0     0     0
     0     2     0     0
     0     0     3     0

Concatenation#

We can also use the square brackets to concatenate vectors and matrices. For example, create two column vectors and put them into the columns of a matrix:

c1 = [1; 2];
c2 = [3; 4];
A = [c1 c2]
A =

     1     3
     2     4

Create two row vectors and put them into the rows of a matrix:

r1 = [0 -1];
r2 = [5 7];
A = [r1; r2]
A =

     0    -1
     5     7

Concatenate matrices to create the block matrix

\[\begin{split} A = \left[ \begin{array}{rrrr} 1 & \phantom{+}1 & 0 & 0 \\ 1 & 1 & 0 & 0 \\ 0 & 0 & -1 & -1 \\ 0 & 0 & -1 & -1 \end{array} \right] \end{split}\]
A = [ones(2,2) zeros(2,2); zeros(2,2) -ones(2,2)]
A =

     1     1     0     0
     1     1     0     0
     0     0    -1    -1
     0     0    -1    -1

Addition and Multiplication#

Use operators + and - for matrix addition and subtraction, and * for scalar multiplication. For example, let’s use eye, ones, diag, matrix addition and scalar multiplication to construct the matrix:

\[\begin{split} A = \left[ \begin{array}{rrrrr} 2 & -1 & 0 & 0 & 0 \\ -1 & 2 & -1 & 0 & 0 \\ 0 & -1 & 2 & -1 & 0 \\ 0 & 0 & -1 & 2 & -1 \\ 0 & 0 & 0 & -1 & 2 \end{array} \right] \end{split}\]
N = 4;
A = 2*eye(N+1) - diag(ones(1,N),1) - diag(ones(1,N),-1)
A =

     2    -1     0     0     0
    -1     2    -1     0     0
     0    -1     2    -1     0
     0     0    -1     2    -1
     0     0     0    -1     2

Use the operator * for matrix multiplication. For example, let’s compute \(A \mathbf{x}\) where \(A\) is the matrix above and \(\mathbf{x}\) is the vector:

\[\begin{split} \mathbf{x} = \begin{bmatrix} 1 \\ 1 \\ 1 \\ 1 \\ 1 \end{bmatrix} \end{split}\]
x = ones(N+1,1);
A*x
ans =

     1
     0
     0
     0
     1

Indexing#

Access the entry of matrix \(A\) at row \(i\) and column \(j\) using the syntax A(i,j). For example, consider the matrix:

A = [1 0 -2; 7 5 -1; 3 4 -8]
A =
     1     0    -2
     7     5    -1
     3     4    -8

Access the entry in row 2 and column 3:

A(2,3)
ans =

    -1

Use the colon : to select an entire row or column. For example, select row 3:

A(3,:)
ans =

     3     4    -8

Select column 2:

A(:,2)
ans =

     0
     5
     4