Department of Engineering

IT Services

Vectors and matrices

You have already started working with vectors and matrices. The array try1 is an example of a matrix, with 2 rows and 3 columns. The horizontal list [-pi/2, 0, pi/2, pi] and the vertical list vlist are both examples of vectors; these can be regarded as special cases of matrices -- a matrix with one row is called a row vector and a matrix with one column is called a column vector.

Matlab has lots of ways of building up matrices, and of extracting bits of matrices. For example, if you type

try2 = [try1 ; 2*try1]

you get a matrix with 4 rows and 3 columns. To pick out the number which is in row 3 and column 2 of try2 you type


To pick out columns 1 and 3 of try2 type

try3 = try2(:,[1,3])

try3 is now a matrix with 4 rows and 2 columns. Here the colon : was used as an abbreviation to mean `all the rows', so that we didn't have to type try2([1,2,3,4],[1,3]).

We can do arithmetic with matrices, but we have to be careful about their sizes. Addition and subtraction operators (+ and -) can be used between any two matrices, providing that they have the same numbers of rows and columns. The meaning is that corresponding elements are added or subtracted. For example

gross_weights = [12.3 23.4 34.5]
crate_weights = [2.3 3.4 4.5]
net_weights = gross_weights - crate_weights

Multiplication and division of corresponding entries can also be obtained, using the operators .* and ./ respectively. Note the `.' here. For example

prices = [1 2 3 4]
quantities = [10 20 30 40]
item_costs = prices .* quantities
total_cost = sum(item_costs)

If you have not seen matrix multiplication or vector scalar products before, you can skip to the end of this section; all the material needed to read the rest of this section is covered in the first year Maths course.

The operator * (without the dot) is used for proper matrix multiplication. Recall that if matrix A has m rows and n columns, and B has p rows and q columns, then the product A*B is defined only if n=p, and the resulting matrix will have m rows and qcolumns. In particular, if A is a row vector (m=1) and B is a column vector (q=1) then the result is just a scalar number, and is in fact the scalar (or `dot', or `inner') product of the two vectors. For example:

weights = [3 1 7]
locations = [-2 0 5]'
cg = (weights * locations) / sum(weights)

Note the use of the transposition operator (') here to make locations into a column vector (locations = [-2;0;5] would have done just as well). Do you see why the centre of gravity is given by using the scalar product in this way? Once you get used to it, lots of calculations can be written in this form. Matlab is particularly efficient at performing scalar product calculations.

Finding inverses of matrices is easy in Matlab. To find the inverse of a random 4-by-4 matrix:

M = rand(4,4)
Minv = inv(M)

To find AB-1 we can write A*inv(B), but Matlab also allows us to write A/B, and to find B-1A it allows B\A as well as inv(B)*A. (Note that this use of / and \ is not standard notation in linear algebra. Using these operators is not quite equivalent to the use of inv, and is in fact preferable on grounds of numerical accuracy - see the Matlab User's Guide for more details.)

There are many more fancy operations on matrices available in Matlab. To find the eigenvalues of M, for example, type:


If you want both the eigenvalues and eigenvectors, type:

[W,D] = eig(M)

In this case W is a matrix whose columns are the eigenvectors, and D is a diagonal matrix, whose diagonal entries are the corresponding eigenvalues. Check that M*W is the same as W*D -- the smart way of doing this is to type M*W - W*D.