Josh Thompson
Postdoc: Colorado State University
Home  >  Teaching
p align="center">Matrices and Vectors

Contents:
1. Basic matrix-vector operations
2. Matrix-vector and matrix-matrix product
3. Linear systems of algebraic equations
4. Plotting arrays


1. Basic matrix-vector operations

1.1 Entering and addressing matrices and matrix elements. In Matlab every numerical quantity is considered to be a complex matrix. In general, a matrix is a rectangular array of numbers with a certain number of rows and a certain number of columns. For example,

 A = 

has 2 rows and 3 columns and is, therefore, referred to as a 2 x 3 matrix. If you want to enter this matrix in Matlab, type

» A=[1,pi,sqrt(-1);sqrt(2),4,0]

A =

   1.0000             3.1416                  0 + 1.0000i
   1.4142             4.0000                  0

Note that commas (alternatively spaces) are used to separate the individual elements in a row, whereas semicolons separate the rows of a matrix. You can address (and access) the number stored in the 1st row, 2nd column by typing

» A(1,2)

ans =

    3.1416

A full row or column is addressed by colons. The commands

» A(:,1),A(2,:)

ans =

    1.0000
    1.4142
 

ans =

    1.4142    4.0000         0

extract the first column and the second row, respectively.
The size of a matrix is the number of rows and columns. For example

» size(A)

ans =

     2     3

tells that A has 2 rows and three columns. Note that even single numbers are considered as matrices. Executing

» size(1)

ans =

     1     1

shows that Matlab considers 1 (or any other number) as a matrix with one row and one column.

1.2 Addition, Subtraction and Scalar Multiplication. If A,B are matrices of the same size they can be added together:

» A=[1 2;3 4];B=[5 6;7 8];C=A+B

C =

     6     8
    10    12

Each element in the matrix C is the sum of the corresponding elements of A and B. Similarly you can subtract matrices of the same size: type C-A to recover B again. You can also multiply every element of a matrix by the same number (scalar multiplication):

» 2*A

ans =

     2     4
     6     8

1.3 Special Matrices. Matlab provides a number of special matrices. For example the commands

» zeros(2,3),ones(2,3),eye(3)

ans =

     0     0     0
     0     0     0
 

ans =

     1     1     1
     1     1     1
 

ans =

     1     0     0
     0     1     0
     0     0     1

generate a 2 X 3 matrix with all entries 0, a 2 X3 matrix with all entries 1, and a 3 X 3 matrix in  which all diagonal elements are 1 and all other elements are zero, respectively. The matrix eye(3)is referred to as the 3 X 3 identity matrix.

1.4 Transposed matrix. You can convert rows to columns (or columns to rows) by means of the transition operator (a single apostrophe):

» A=[1 2 3;4 5 6];A',ans'

ans =

     1     4
     2     5
     3     6
 

ans =

     1     2     3
     4     5     6

1.5 Elementwise multiplication,  division and powers. A very useful tool offered by Matlab is elementwise multiplication and division. As with addition, these operations can only be applied to matrices of the same size. Let's first define two such matrices:

» A=[2 4 6;8 10 12],B=[1 2 3;4 5 6],

A =

     2     4     6
     8    10    12
 

B =

     1     2     3
     4     5     6

Now we multiply each element of A to the corresponding element of B,

» A.*B

ans =

     2     8    18
    32    50    72

and divide each element of A by the corresponding element of B:

» A./B

ans =

    2.0000    2.0000    2.0000
    2.0000    2.0000    2.0000

Elementwise multiplication and division is marked by the period preceeding the multiplication and division symbols. The division  command above is also called left division because Matlab allows to reverse the order. The right division command B.\A produces the same answer as above (try it).

Finally assume we want to raise each element of A to its square (or any other power). To do this  the  exponentiation symbol must be preceeded by a period:

» A.^2

ans =

     4    16    36
    64   100   144

If you want to raise different  elements of A to different powers, you can write  the powers into
a matrix, say C, of the same size as A and then apply the command A.^C.

1.6 Vectors. A vector is just a list of numbers. If the list is vertical it is called a column vector, if the list is horizontal it is called a row vector. Thus a vector is a special case of a matrix which has only one column or only one row. For example,

» u=[1;2;3;4],v=[1,2,3,4]

u =

     1
     2
     3
     4
 

v =

     1     2     3    4

generate column and row vectors with the same numbers. With the tranpose operator you can transform row vectors into column vectors and conversely as for matrices (try u', v'). Similarly you can add and subtract vectors of the same size, which here means that the vectors must  be of the same type (rows or columns) and have the same number of elements. With u,v as defined above, the command u+v produces an error message. Adressing and accessing a single element or a segment of elements in a vector works in the same manner as for matrices, recalling that vectors are nothing than special matrices.

The commands

» v(3),v(2:3),v(2:end)

ans =

     3
 

ans =

     2     3
 

ans =

     2     3     4

generate the 3rd element, a row vector consisting of the 2nd up to the third element, and a row vector consisting of all elements from the 2nd to the last element, respectively.

Two of the above commands through which special matrices are created work for vectors as well:

ones(1,5)
and
zeros(1,5)

generate row vectors with 5 elements, each  1 and 0, respectively. With ones(5,1) and zeros(5,1) one can generate the corresponding column vectors.

In addition, Matlab offers the possibility to generate row vectors of arbitrary length associated with a given interval:

x=linspace(1,2 100)

generates a row vector that consists of 100 numbers distributed uniformly in the interval 1£ x £ 2, with x(1)=1, x(100)=2.

x=1:2:0.01

generates a row vector starting at x(1)=1 and with the elements successively increased by 0.01 until the largest number which is smaller than or equal to 2 is reached.

The size command applied to a row  or column vector of length 4, say, yields the vector [1 4]
or [4 1]. If you know already that your object is a vector, the output 1 in this answer does not tell you anything new. Therefore Matlab offers the command length to extract the  information about the length of a vector:

» length(u)

ans =

     4
tells that your vector has length 4. When applied to matrices with more than one row and one column, this command yields the larger of the 2 numbers contained in size(A).

2. Matrix-vector and matrix-matrix product

Given n vectors a1a2  .... an  of the same size and n scalars (numbers)  x1, x2 ,...., xn , the vector x1a1 +  x2a2  +.... +  xnanis called a linear combination of the vectors a1 a2  ....  an. For example, defining the column vectors
a1 a2 a3 and the numbers x1  x2  x as

» a1=[2;-2;2];a2=[4;3;4];a3=[-3;2;1];x1=1;x2=0;x3=-2;

the associated linear combination can be defined through

» x1*a1+x2*a2+x3*a3

ans =

     8
    -6
    -4

The linear combination concept leads naturally to the definition for matrix - vector multiplication. One multiplies a matrix A to a column vector x simply by taking the linear combination of the columns of A, using the entries of the vector x for the scalars in the linear combination. It is important to note that this definition requires that the length of the vector, to which the matrix is multiplied, coincides with the number of columns of the matrix.

Let's first associate to the three vectors above a matrix  which has these vectors as columns,

» A=[a1 a2 a3]

A =

     2     4    -3
    -2     3     2
    -2     4     1

and collocate the numbers x1  x2  x3 in a column vector:

» x=[x1;x2;x3];

Execution of the command

» A*x

ans =

     8
    -6
    -4

produces the same vector as before.

Another way of viewing the matrix-vector multiplication is that the j-th entry of the resulting vector is the product of the j-th row of A and the vector x. Note that the product of a row to a column vector is the linear combination of the individual numbers of the row and so is a number. You can check this by multiplying the 2nd row of A to x:

» A(2,:)*x

ans =

    -6

The definition of matrix-vector multiplication is easily extended to matrix-matrix multiplication. The matrix product AB=C just means that A is multiplied to each column vector of  B and that the resulting column vectors form the columns of the product matrix C. Define two column vectors:

» b1=x;b2=[2;2;2];

multiply A to each of them individually and collocate the two vectors in a 3 X 2 matrix:

» c1=A*b1;c2=A*b2;[c1 c2]

ans =

     8     6
    -6     6
    -4     6

You get the same result by first associating with the column vectors b1,b2 the matrix B=[b1 b2]
and then forming the matrix multiplication AB:

» B=[b1 b2];A*B

ans =

     8     6
    -6     6
    -4     6

Instead of just 2 columns, the matrix B can have an arbitrary number of columns, but their lengths (number of rows of B)must be equal to 3, the number of columns of matrix A.The resulting product matrix C then has the same number of columns as B and the same number of rows as A.

Summary: The matrix multiplication C=AB is defined if the number of columns of A coincides with the number of rows of B. Then the number of rows of C is the same as the number of rows of A and the number of columns of C is the same as the number of columns of B.

Short: the product AB of a m x n matrix A and a n x k Bmatrix is an m x k matrix.

3.  Linear systems of algebraic equations

A system of equations of the form

a11x1  + a12 x2   + .... + a1nxn   =  b1
a21x1  + a22 x2   + .... + a2nxn   =  b2
    .                                .              .
    .                                .              .
am1x1  + am2 x2   + .... + amnxn  =  bm

with given coefficients aij  and given numbers bj   is called a linear system of m equations for the n unknowns xi . Collocating the bj in a column vector b of length m and the xi in a column vector x of length n, this system can be compactly written in the form Ax = bwhere the m x n matrix A contains the coefficients aij.  Matlab allows to handle such systems  with an arbitrary number of equations and unknowns.  The solution is based on the so called Gaussian elimination algorithm which is discussed in linear algebra courses such as M229 and M369 (see als Section 5.1 of  the textbook).  In M340 we will mainly deal with linear systems of equations with the same number of equations and unknowns. In this case the numbers of rows and columns of A are the same, i.e. m=n. Then a (necessary and sufficient) condition for Ax = b to have a unique solution for any (target) vector b is that the determinant of A be nonzero. In most cases (if you have set up the equations correctly), this criterion will be met so that there is a unique solution.

Let's check if our matrix A defined above meets the condition for a unique solution:

» det(A)

ans =

   -12

Since the determinant is nonzero, we can solve Ax = b for any b. We choose for  b the vector c1 introduced before and solve for x as follows:

» x=A\c1

x =

     1
     0
    -2

By checking Ax,

» A*x

ans =

     8
    -6
    -4

we see that this reproduces our vector c1.
Note that the solution to Ax = b can be formally written as x=A-1b . The matrix  A-1  is the inverse matrix of A which satisfies AA-1 = A-1A = I, where I is the 3  x  3 identity matrix (the matrix eye(3)introduced earlier). Matlab gives you access to the inverse matrix:

» inv(A)

ans =

    0.4167    1.3333   -1.4167
    0.1667    0.3333   -0.1667
    0.1667    1.3333   -1.1667

» A*inv(A)

ans =

     1     0     0
     0     1     0
     0     0     1

and you may also use this matrix to find the solution to Ax = b, but the preferred method is to apply the   "backslash-operation" (x=A\c1).

4. Plotting arrays

The plot command is reserved for plotting vectors. If x and y are vectors of the same length (not necessarily of the same type, i.e. x can be a row and y can be a column), the command plot(x,y) interprets the numbers stored in x as x-coordinates and the numbers stored in y as y-coordinates of points in a plane, and connects these points by straight line segments. For example, in the commands

» x=linspace(-pi,pi,200);y=sin(x);plot(x,y)

first a vector of 100 uniformly distributed points in the interval - x £ p  is generated, then the sines of  these points are computed and stored in the vector y,and finally the two arrays are plotted versus each other. You can also create several graphs in one plot, e.g.

» plot(x,y,x,y.^2)

graphs sin(x) and sin(x)2 in the same plot. Alternatively you can work with the hold on command:

» hold on, plot(x,y), plot(x,y.^2)

to obtain the same result. You can adjust the styles of the individual plots, for example

» plot(x,y,'k',x,y.^2,'k--')

plots both graphs in black (adjusted by 'k') and the second graph dashed (adjusted by --). Type help plots for further details.

Another way to create multiple plots is to plot x versus a matrix whose rows (or columns) have the same length as the basic vector x.For example,

» plot(x,[y',y'.^2])

plots x versus the first and the second column of the matrix [y',y'.^2]and hence generates the same multiple plot as  before.

If many graphs have to be generated in one plot, it makes sense to use a matrix. Suppose, for example, we want to plot the function family

y(x) = ae-x + 2xe-x

as function of x for values of a ranging from -5 to 5 in steps of 2. This can be done using the following commands (executed in a script):

a=-5:2:5;
x=linspace(-3,3,200);
Y=a'*exp(-x)+2*ones(6,1)*(x.*exp(-x));
plot(x,Y),axis([-3 3 -5 10]);

Here the function data are stored in the rows of the matrix Y, and then x is plotted versus (each row of) Y. The axis command restricts the horizontal range to x between -3 and 3 and the vertical range to y between -5 and 10.
The same plot can be generated using a loop and the hold on command

x=linspace(-3,3,200);
hold on
for a=-5:2:5;
   y=a*exp(-x)+2*x.*exp(-x);
plot(x,y),axis([-3 3 -5 10]);
end

but note the different colors. With the first command the function data have been stored and thus can be accessed again and used  for other plots. For example,

» plot(x,Y(2,:))

plots the function for a=-3.