Contents

2. Vectors and matrices

The objects most of interest to us for now are vectors and matrices.

Definition 2.1 (Vector)

A vector is a finite collection of numbers called its elements. The set of all vectors with \(n\) real-valued elements is denoted \(\real^n\). If the elements are complex numbers, we use the name \(\complex^n\).

Warning

Complex numbers are coming. They play an essential role in understanding dynamics.

I use boldfaced lowercase letters to represent vectors, and a subscript to refer to an individual element within one. For instance, \(x_3\) is the third element of a vector \(\bfx\).

There is one more big definition to get out of the way before we really dive in.

Definition 2.2 (Matrix)

A matrix is an \(m\times n\) array of numbers called its elements. The set of all \(m\times n\) matrices with real elements is denoted \(\rmn{m}{n}\), and with complex elements it’s \(\cmn{m}{n}\).

I use boldfaced uppercase letters to represent vectors, and a pair of subscripts to refer to an individual element within one. For instance, \(A_{23}\) is the element in row 2, column 3 of the matrix \(\bfA\).

Note

Matrix subscripts are always in the order row first, column second. (This is the opposite of Excel, which is just one of myriad reasons that Excel is evil.)

As you might suspect, there’s a lot more to vectors and matrices than these innocent definitions let on. The consequences of making algebraic rules that bind vectors and matrices to describe linear functions will comprise our brief tour of linear algebra.

2.1. MATLAB

MATLAB stands for “matrix laboratory,” and it’s a natural place to compute with vectors and matrices. One caution, though: it operates with numbers of finite precision, like a calculator, rather than with exact expressions like Mathematica. Hence things that are supposed to be equal might only be approximately so.

To construct small vectors and matrices, put elements inside square brackets. Spaces separate columns, and semicolons separate rows.

x = [ 1 2 3 4 ]
y = [10; 20; 30]
A = [ 1 2 3; -4 -5 -6; pi sqrt(2) exp(1) ]
ans =

    '9.7.0.1296695 (R2019b) Update 4'
x =

     1     2     3     4
y =

    10
    20
    30
A =

   1.000000000000000   2.000000000000000   3.000000000000000
  -4.000000000000000  -5.000000000000000  -6.000000000000000
   3.141592653589793   1.414213562373095   2.718281828459046

Mathematical constants

The constant \(\pi\) is defined automatically as pi. The constant e is not defined, and expressions like \(e^{-2}\) causes an error if you have not previously defined e. Instead, use the exponential function exp; note that \(e=\exp(1)\).

As you can see from this output, MATLAB allows two kinds of vector: a row vector, which has a single row, and a column vector, which has a single column. The distinction doesn’t always matter, but when it does, it really does. We are going to make a blanket assumption that serves well most of the time.

Note

All vectors are assumed to have a column shape.

We can get the dimensions of a vector using size.

sizes = [ size(x); size(y); size(A) ]
sizes =

     1     4
     3     1
     3     3

You can see from the above that we can concatenate vectors and matrices inside square brackets. Also:

Note

MATLAB makes no distinction between a column vector in \(\real^n\) and an \(n\times 1\) matrix in \(\rmn{n}{1}\).

To access individual elements in a vector or matrix, use parentheses. The end keyword always stands for the last element in its dimension.

x_2 = x(2)
A_31 = A(3,1)
y_last = y(end)
x_2 =

     2
A_31 =

   3.141592653589793
y_last =

    30

The parenthetical syntax also works for assigning values within a vector or matrix.

A(2,1) = 1i     % the imaginary unit
A =

  Column 1

  1.000000000000000 + 0.000000000000000i
  0.000000000000000 + 1.000000000000000i
  3.141592653589793 + 0.000000000000000i

  Column 2

  2.000000000000000 + 0.000000000000000i
 -5.000000000000000 + 0.000000000000000i
  1.414213562373095 + 0.000000000000000i

  Column 3

  3.000000000000000 + 0.000000000000000i
 -6.000000000000000 + 0.000000000000000i
  2.718281828459046 + 0.000000000000000i

Note how complex numbers are automatically supported.