Department of Engineering

IT Services

Matrices

So far you have operated on scalars. MATLAB provides comprehensive matrix operations. Type the following in and look at the results:


>> a=[3 2 -1

0 3 2
1 -3 4]
>> b=[2,-2,3 ; 1,1,0 ; 3,2,1]
>> b(1,2)
>> a*b
>> det(a)
>> inv(b)
>> (a*b)'-b'*a'
>> sin(b)

The above shows two ways of specifying a matrix. The commas in the specification of b can be replaced with spaces. Square brackets refer to vectors and round brackets are used to refer to elements within a matrix so that b(x,y) will return the value of the element at row x and column y. Matrix indices must be greater than or equal to 1. det and inv return the determinant and inverse of a matrix respectively. The ' performs the transpose of a matrix. It also complex conjugates the elements. Use .' if you only want to transpose a complex matrix.

The * operation is interpreted as a matrix multiply. This can be overridden by using .* which operates on corresponding entries. Try these :


>> c=a.*b

>> d=a./b
>> e=a.^b

For example c(1,1) is a(1,1)*b(1,1). The Inf entry in d is a result of dividing 2 by 0. The elements in e are the results of raising the elements in a to the power of the elements in b. Matrices can be built up from other matrices:


>> big=[ones(3), zeros(3); a , eye(3)]

big is a 6-by-6 matrix consisting of a 3-by-3 matrix of 1's, a 3-by-3 matrix of 0's, matrix a and the 3-by-3 identity matrix.

It is possible to extract parts of a matrix by use of the colon:


>> big(4:6,1:3)

This returns rows 4 to 6 and columns 1 to 3 of matrix big. This should result in matrix a. A colon on its own specifies all rows or columns:


>> big(4,:)

>> big(:,:)
>> big(:,[1,6])
>> big(3:5,[1,4:6])

The last two examples show how vectors can be used to specify which non-contiguous rows and columns to use. For example the last example should return columns 1, 4, 5 and 6 of rows 3, 4 and 5.