Sei sulla pagina 1di 18

Matrices 5.

1

5. Matrices: two-dimensional arrays
Just as in the case of row and column vectors, which may be considered to be special
cases of matrices, MATLAB has the usual operations for two-dimensional matrices.
It also has the extra facilities for array processing.
An m n matrix is a rectangular array of numbers having m rows and n columns. It is
usual in a mathematical setting to include the matrix in either round or square
brackets; we shall use square ones. For example, when 3, 2 m n = = we have a 3 2
matrix such as
1 2
2 3
4 1
(
(
=
(
(

A
To enter such a matrix into MATLAB we type it in row by row using the same syntax
as for vectors:
>> clear all
>> A=[-1, -2 % press return to enter new numbers
2, 3
4, -1]
A =
-1 -2
2 3
4 -1
>> clear all
>> A=[-1, -2; 2, 3; 4, -1]
A =
-1 -2
2 3
4 -1
or
>> B=[2:6; -2:2; 13:2:21]
B =
2 3 4 5 6
-2 -1 0 1 2
13 15 17 19 21

Matrices 5.2

Size of a matrix
We can get the size (dimensions) of a matrix with the command size
>> size(A), size(B)
ans =
3 2
ans =
3 5
so A is 3 2 and B is 3 5 . The last command shows that the value returned by size
is itself a 1 2 matrix, a row vector.
Transpose of a matrix
Transposing a vector changes it from a row to a column vector and vice versa. The
extension of this idea to matrices is that transposing interchanges rows with the
corresponding columns: the 1
st
row becomes the 1
st
column, and so on. Remember
that we use
T
for trasposition in matrix algebra but in MATLAB we use .
>> A, A'
A =
-1 -2
2 3
4 -1
ans =
-1 2 4
-2 3 -1

5.1 Special matrices
MATLAB provides a number of useful built-in matrices of any desired size.
ones(m,n) gives an m n matrix of 1s.
>> wun=ones(2,4)
wun =
1 1 1 1
1 1 1 1
zeros(m,n) gives an m n matrix of 0s.
>> nun=zeros(2,4)
nun =
0 0 0 0
0 0 0 0
Matrices 5.3

We can construct a matrix in many ways, e.g.
>> ones(size(A))
ans =
1 1
1 1
1 1
>> Z=[zeros(2,3),ones(2,2)]
Z =
0 0 0 1 1
0 0 0 1 1
An n n matrix that has the same number of rows and columns is called a square
matrix.
A matrix is said to be symmetric if it is equal to its transpose, i.e. it is unchanged by
transposition.
>> C=[2, -3, 0; -3, 4, 1; 0, 1, 5]
C =
2 -3 0
-3 4 1
0 1 5
>> C_trans=C'
C_trans =
2 -3 0
-3 4 1
0 1 5
>> C-C_trans
ans =
0 0 0
0 0 0
0 0 0

5.2 The identity matrix
The n n identity matrix is a matrix of zeros except for having ones along its leading
diagonal (top left to bottom right). This is called eye(n) in MATLAB (since
mathematically it is usually denoted by I).
>> clear all
Matrices 5.4

>> I=eye(4), b=[-1; 2; 3; -4], I*b
I =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
b =
-1
2
3
-4
ans =
-1
2
3
-4
Notice that multiplying the 4 1 vector b by the 4 4 identity I has no effect, it is
equivalent to multiplying a number by 1.

5.3 Diagonal matrices
A diagonal matrix is similar to the identity matrix except that its diagonal entries are
not necessarily equal to 1.
3 0 0
0 1 0
0 0 7
(
(
(
(


is a 3 3 diagonal matrix. To construct this in MATLAB, we could either type it in
directly
>>clear all
>> D=[-3, 0, 0; 0, 1, 0; 0, 0, 7]
D =
-3 0 0
0 1 0
0 0 7
but this becomes totally impractical when the dimension is large, e.g. even a 10 10
diagonal matrix. We use the diag function. First define a vector d, row or column,
containing the values of the diagonal entries in order, then diag(d) gives the required
matrix.
Matrices 5.5

>> clear all
>> d=[-3, 1, 7], D=diag(d)
d =
-3 1 7
D =
-3 0 0
0 1 0
0 0 7
If A is any matrix, the command diag(A) extracts its leading diagonal entries:
>> clear all
>> A=[2, 5, -3, 1; 5, -1, 2, 9;, 6, -1, 4,-2]
A =
2 5 -3 1
5 -1 2 9
6 -1 4 -2
>> diag(A)
ans =
2
-1
4

5.4 Building matrices
It is often convenient to build large matrices from smaller ones.
>> clear all
>> B=[1, 2; 5, 6; 3, 2], c=[-2; -1; -3]
B =
1 2
5 6
3 2
c =
-2
-1
-3
>> F=[B, c]
F =
1 2 -2
5 6 -1
3 2 -3
We have added an extra column c ( ) 3 1 to B ( ) 3 2 in order to form F ( ) 3 3
Matrices 5.6

>> G=[B'; c']
G =
1 5 3
2 6 2
-2 -1 -3
We have stacked
T
B ( ) 2 3 on top of
T
c ( ) 1 3 to form G ( ) 3 3 .
Heres a more complicated one:
>> A=[2:5; 9:12; 10, 3, 2, 1; -1:-2:-7]
A =
2 3 4 5
9 10 11 12
10 3 2 1
-1 -3 -5 -7
>> B=[diag(6:9), A; A', ones(4,4)]
B =
6 0 0 0 2 3 4 5
0 7 0 0 9 10 11 12
0 0 8 0 10 3 2 1
0 0 0 9 -1 -3 -5 -7
2 9 10 -1 1 1 1 1
3 10 3 -3 1 1 1 1
4 11 2 -5 1 1 1 1
5 12 1 -7 1 1 1 1
The command spy(B) will produce a graphical display of the location of the nonzero
entries in B, it also gives a value for nz , the number of nonzero entries. See Figure
5.1.
>> spy(B)
Matrices 5.7

Figure 5.1. spy(B) for the matrix B.

5.5 Tabulating functions
We have already considered function tabulation but we are now in a position to
produce a more suitable tabular format.
Tabulate the functions cos2 u x x = and 0.5
x
v e = for 0, 0.1, 0.2,..., 0.5 x = .
>> clear all
>> x=0:0.1:0.5;
>> u=x.*cos(2*x); v=0.5*exp(x);
>> [x', u', v']
ans =
0 0 0.5000
0.1000 0.0980 0.5526
0.2000 0.1842 0.6107
0.3000 0.2476 0.6749
0.4000 0.2787 0.7459
0.5000 0.2702 0.8244
Note that we use the tranpose, to obtain the column vectors. We could, of course,
replace the last command by [x; y; u;]. We could also have done this more
directly as follows:
>> clear all
>> x=(0:0.1:0.5)';
>> [x, x.*cos(2*x), 0.5*exp(x)]
ans =
Matrices 5.8

0 0 0.5000
0.1000 0.0980 0.5526
0.2000 0.1842 0.6107
0.3000 0.2476 0.6749
0.4000 0.2787 0.7459
0.5000 0.2702 0.8244

5.6 Extracting bits of matrices
We can extract sections from a matrix in much the same way as for a vector. Each
element of a matrix is indexed according to the row and column to which it belongs.
The entry in the ith and jth column is denoted by
, i j
A . In MATLAB it is denoted by
A(i,j).
>> clear all
>> A=[2:2:8; 7:-1:4; 0, 1, 2, 3; -3:1:0]
A =
2 4 6 8
7 6 5 4
0 1 2 3
-3 -2 -1 0
>> A(1,1), A(3,4), A(4,2), A(5,3)
ans =
2
ans =
3
ans =
-2
??? Index exceeds matrix dimensions.
In the following examples we extract
(i) the 4
th
column,
(ii) the 2
nd
and 3
rd
columns,
(iii) the 3
rd
row, and
(iv) the upper 2 2 matrix.
>> A(:,4) % col. 4
ans =
8
4
3
0
Matrices 5.9

>> A(:,2:3) % cols 2 to 3
ans =
4 6
6 5
1 2
-2 -1
>> A(3,:) % row 3
ans =
0 1 2 3
>> A(1:2, 1:2) % rows 1 to 2 and cols 1 to 2
ans =
2 4
7 6
Thus, : on its own refers to the entire column or row depending on whether it is the
first or the second index.

5.7 find for vectors and matrices
The function find returns a list of the positions, i.e. the indices, of the elements of a
vector satisfying a given condition.
>> clear all
>> x=-pi:pi/20:pi;
>> y=exp(-x.^2).*sin(2*x); plot(x,y,'-')
>> hold on, plot(x(k), y(k), 'ro')
>> x=-pi:pi/20:pi;
>> y=exp(-x.^2).*sin(2*x); plot(x,y,'-')
>> k=find(y>0)
k =
Columns 1 through 9
1 2 3 4 5 6 7 8 9
Columns 10 through 18
10 22 23 24 25 26 27 28 29
Columns 19 through 20
30 31
>> x=-pi:pi/20:pi;
>> y=exp(-x.^2).*sin(2*x); plot(x,y,'-')
>> k=find(y>0.1)
k =
22 23 24 25 26 27 28 29
>> hold on, plot(x(k), y(k), 'ro')
Matrices 5.10

>> k1=find(x<0 & y<0)
k1 =
Columns 1 through 9
11 12 13 14 15 16 17 18 19
Column 10
20
>> plot(x(k1), y(k1), 'g*')
Figure 5.2.
2
sin2
x
y e x t

=
We shall find elements of a matrix, A, using the find command.
>> clear all
>> A=[1:2:7; 0, -1, 0, 0; -4:4:8]
A =
1 3 5 7
0 -1 0 0
-4 0 4 8
>> b=find(A==0), c=find(A<0)
b =
2
6
8
11
c =
3
5
Matrices 5.11

>> A(b), A(c)
ans =
0
0
0
0
ans =
-4
-1
Thus we find that A has elements equal to 0 in positions 2, 6, 8 and 11 and it has
negative elements in positions 3 and 4. To interpret these results we notice that find
first reshapes A into a column vector and this is equivalent to numbering the elements
of A by columns as in
1 4 7 10
2 5 8 11
3 6 9 12

Also we see that b and c return the locations of the elements which are zero and
negative respectively.
Now lets consider the transpose.
>> d=find(A'==0)
d =
5
7
8
10
and since we are dealing with A, the entries are numbered by rows.

5.8 Array product of matrices (.*)
The array product works just as it does for vectors. Corresponding elements are
multiplied together, consequently matrices involved must have the same size.
>> clear all
>> A=[2, -1, 3; 0, 1, 4], B=[-2, 3, 2; 4, -1, 1], C=[2, 1;
-3, 4]
A =
2 -1 3
0 1 4
B =
Matrices 5.12

-2 3 2
4 -1 1
C =
2 1
-3 4
>> A.*B
ans =
-4 -3 6
0 -1 4
>> A.*C
??? Error using ==> .*
Matrix dimensions must agree.

5.9 Matrix-vector products
The matrix-vector product is defined only for column vectors that have the same
number of entries as the matrix has columns. So, if A is an m n matrix and x is a
column vector of length n, then we can formthe matrix-vector product Ax.
An m n matrix times an 1 matrix an 1 n m matrix.
We visualise A as being made up of m row vectors stacked on top of each other, then
the product corresponds to taking the scalar product of each row of A with the vector
x. The result is a column vector with m entries.

( ) ( )
( )
2
2 1 3
3
0 1 4
1
2 2 1 3 3 1
0 2 1 3 4 1
4
7
(

(
(
=
(
(
(


+ + (
=
(
+ +
(

(
=
(

Ax

In MATLAB it is easy to develop
>> clear all
>> x=[-2; 3; 1]
x =
-2
3
1
>> A=[2, -1, 3; 0, 1, 4]; A*x
ans =
Matrices 5.13

-4
7
( )
( ) ) times ( 1 1 . m n n m
It is important to recognise that matrix-vector multiplication is not commutative i.e.
= Ax xA, and hence A*x is not the same as x*A.

5.10 Matrix-matrix products
To form the product of an m n matrix A and an n p matrix B, written as AB, we
visualise the first matrix A as being composed of m row vectors of length n stacked on
top of each other while the second B is visualised as being made up of p column
vectors of length n:

columns
rows ,
p
m
(
(
(
(
(

(
= =
(
(
(
(

(

(

A B
The entry in the ith row and jth column of the product is then the scalar product of the
ith row of A with the jth column of B. The product is an m p matrix:
>> clear all
>> A=[1, -2, 3; 2, 0, 1], B=[-1, 2; 2, 4; -1, 0]
A =
1 -2 3
2 0 1
B =
-1 2
2 4
-1 0
>> C=A*B
C =
-8 -6
-3 4
>> D=B*A
D =
3 2 -1
10 -4 10
-1 2 -3
Matrices 5.14

Notice that is 2 3 A and is 3 2 B so that is 2 2 AB and is 3 3 BA .
Now lets consider the transpose.
>> (A*B)'
ans =
-8 -3
-6 4
>> B'*A'
ans =
-8 -3
-6 4
and this suggests that
( )
T
T T
= AB B A

5.11 Determinants
The determinant of a square matrix A is found using the command det.
We shall find the determinant of the 3 3 Pascal matrix, P.
>> clear all
>> P=Pascal(3)
P =
1 1 1
1 2 3
1 3 6
>> det(P)
ans =
1
A square matrix, A, whose determinant is zero is called a singular matrix. If det(A)
is non-zero then A is said to be non-singular.

5.12 The inverse matrix
A square matrix A has an inverse,
1
A , if and only if it is non-singular i.e.
( ) det 0 = A .
We have shown that for the Pascal matrix, P, ( ) det 0 = P and hence is non-singular.
We shall find the inverse of P.
Matrices 5.15

>> inv(P)
ans =
3 -3 1
-3 5 -2
1 -2 1
Check that
1 1
= = P P PP I :
>> inv(P)*P, P*inv(P)
ans =
1 0 0
0 1 0
0 0 1
ans =
1 0 0
0 1 0
0 0 1
For small systems of equations, no larger than 3 3 , the inverse matrix may be used
to solve systems of linear equations.
Solve the following system of linear equations:
6
2 3 14
4 9 36
x y z
x y z
x y z
+ + =
+ + =
+ + =

In matrix form we write the equations as Ax = b and using the inverse matrix we can
write the solution as
1
x = A b .
>> A=[1, 1, 1; 1, 2, 3; 1, 4, 9]; b=[6; 14; 36];
>> x=inv(A)*b
x =
1
2
3
so 1, 2, 3 x y z = = = .
However, while this is a convenient algebraic approach, finding the inverse is a very
inefficient process and in practice is never done to solve equations larger than 3 3 .
In MATLAB we use the backslash operator \ which solves the system using an
efficient version of the LU algorithm.
Matrices 5.16

>> x=A\b
x =
1.0000
2.0000
3.0000

5.13 Eigenvalues and eigenvectors
Find the eigenvalues and corresponding eigenvectors of the matrix
1 1 2
1 2 1
0 1 1
(
(
=
(
(

A
>> A=[1, 1, -2; -1, 2, 1; 0, 1, -1];
>> [V,D]=eig(A)
V =
0.3015 -0.8018 0.7071
0.9045 -0.5345 -0.0000
0.3015 -0.2673 0.7071
D =
2.0000 0 0
0 1.0000 0
0 0 -1.0000
V is a matrix whose columns are the eigenvectors and D is a diagonal matrix
containing the corresponding eigenvalues along the diagonal.
So the eigenvalues and corresponding eigenvectors of A are

0.3015 0.8018 0.7071
2, 0.9045 ; 1, 0.5345 ; 1, 0
0.3015 0.2673 0.7071
( ( (
( ( (

( ( (
( ( (


and the eigenvectors have been normalised so that the sum of the squares of the
elements is 1.

Matrices 5.17

5.14 Least squares fitting
We consider fitting a straight line of the form
0 1
y a a x = + to a set of N data points
( ) ,
i i
x y .
The normal equations are
0
2
1
i i
i i i i
N x y a
x x x y a
( (
(
=
( (
(





which may be written, after dividing by N, as
( )
0
2
1
1 x
y
a
a
x x xy
(
(
(
( =
(
(
(



Rather that input a long list of data to illustrate the approach, we shall generate a
random set of numbers between 0 and 1 and then sort them. We shall then find the
least squares straight line and plot the data points and the line.
>> N=50;
>> y=sort(rand(1,N)); % N random numbers between 0 and 1
>> h=1/(N-1); x=0:h:1;
>> A=[1, mean(x); mean(x), mean(x.^2)]
A =
1.0000 0.5000
0.5000 0.3367
>> b=[mean(y); mean(x.*y)]
b =
0.5476
0.3522
>> a=A\b
a =
0.0958
0.9035
Matrices 5.18

>> Y=a(1)+a(2)*x; plot(x,Y,x,y,'+')
Figure 5.3. Least squares straight line approximation to random data.

Potrebbero piacerti anche