Você está na página 1de 32

Matrices

Week 8

By Ji Hui
Matrices
• Matlab stands for “Matrices Laboratory” 
– Specified to work with data in the form of matrix
• Meaning of Matrix
– An arrangement of data in rows and columns, e.g. 
a table.
– A mathematical object, particular mathematical 
operations are defined in linear algebra: e.g. 
matrix multiplication
Example of Matrices
• Matrices
 2 1
 0 5 3 1
A=  0 5  B=  
  2 0 9 6 
 4 8 
Dimension: 3 x 2.                    Dimension: 2x4           
• Vectors 1
2
– Special case of matrix with
c = 
ony one row (or only one column  3
 
d = 6 3 2   4
Example of matrix data
• A ready‐mix concrete company has three factories (S1, S2
and S3) which must supply three building sites (D1, D2
and D3). The costs, in some suitable currency, of
transporting a load of concrete from any factory to any
site are given by the following cost table:

D1 D2 D3
S1 3 12 10
S2 17 18 15
S3 7 10 24
Reviews on matrix and its operations
• Index, elements and matrix

,
Matrix algebra
• Transpose

, ,
(cont’)
• Matrix addition

, , ,

• Scale multiplication

, ,
(cont’)
• Matrix multiplication

, , ,
(cont‘)
• Special matrix 

• No division for matrices
• If A is invertible square matrix, there exist a 
matrix  , s.t.
Creating matrix
• Creating vector
– use comma (or empty space) to separate items
– e.g. [1,8,4,0] or [1 8 4 0]
• Creating matrix
– use a semicolon to indicate the end of a row
– e.g. >> a = [1,2,3; 4, 5,6]
a =
1     2    3
4 5    6
(cont’)
– Constructing big matrix from smaller matrices
– e.g. >> c = [1 2; 3 4];
>> x = [5 6]; 
c 1    2
>> a = [c; x]
a = = 3    4
b =
x 5  6
1     2     5  
3     4     6
Subscripts
• Individual elements of a matrix are referenced 
with two subscripts, the first for the row, and 
the second for the column,
– e.g. a(2,3) refers elements at 2‐th row and 3‐th 
column
>> a(2,3)
ans =
6
(cont’)
• Alternatively, you may use a single index by 
think a matrix as being “unwounded” column 
by column
– e.g.  >> a(3)
ans 1   
= 2 4  
1   2   3 2
4   5   6  5
3
6
The colon operator for subscripts
• The colon operator is extremely powerful, and 
provides for very efficient ways of handling 
matrices
– e.g. if a is the matrix 1 2   3
4 5   6
(1)  >> a(2:3,1:2) 7    8   9
ans = 
4 5
7 8
% returns second and third rows, first and second 
columns
(cont’)
(2) >>a(2,:)  1 2   3
ans = 4 5   6
7   8   9 7    8   9
% returns the whole second row 
(3) >> a(1:2,2:3) = [0,0;0,0]
ans =
1     0     0
4     0     0
7     8     9
%  replaces the 2‐by‐2 submatrix composed of the first and 
second row and  the second and third column with a 
square  matrix of 0’s).
What is happening using colon 
operator
• the colon operator is being used to create vector 
subscripts. 
• However, a colon by itself in place of a subscript 
denotes all the elements of the corresponding 
row or column. 
• You can use vector subscripts to get more 
complicated effects,
– e.g. a(:,[1 3]) = b(:,[4 2]) replaces the first and third 
columns of a by the fourth and second columns of b (a 
and b must have the same number of rows).
• What happen to a(:) 
The keyword end in subscripts.
• The keyword end refers to the last row or 
column of an array. 
– e.g. >> a[2,2:end] 1 2   3
ans =  4 5   6
5   6 7    8   9
Elementary matrices
• There is a group of functions to generate 
‘elementary’ matrices, See help elmat
– the functions zeros(n), ones(n) and rand(n) generate 
n‐by‐n matrices of 1’s, 0’s and random numbers, 
respectively.
>> zeros(3)
ans = 
0   0   0
0   0   0
0   0   0
(cont’)
– function eye(n) generates an n×n identity matrix
>> eye(3)
ans =  
1  0  0
0  1  0
0  0  1
An example to construct tri‐diagonal 
matrix
>> a = 2 * eye(5);
>> a(1:4, 2:5) = a(1:4, 2:5) + eye(4);
>> a(2:5, 1:4) = a(2:5, 1:4) + eye(4)
a =
2 1 0 0 0
1 2 1 0 0
0 1 2 1 0
0 0 1 2 1
0 0 0 1 2
transpose
• The transpose operator ’ (apostrophe) turns rows 
into columns and vice versa.
– e.g. >> a=[1,2,3;4,5,6]
a =
1     2     3
4     5     6
>> a'
ans =
1     4
2     5
3     6
Changing shape of matrices
• The function  reshape(x,m,n) returns the m‐by‐n 
matrix whose elements are taken column‐wise from x
– e.g. >> a=[1 2 3;4 5 6]
a =
1     2     3  1
4     5     6 1 2   3 4 1  5
>> reshape(a,3,2) 4 5   6 2 4  3
ans = 5 2  6  
1     5 3
4     3 6
2     6
Manipulating matrices
• Here are some functions for manipulating matrices. See help for 
details.
• Diag
– extracts or creates a diagonal.
• Fliplr
– flips from left to right.
• Flipud
– flips from top to bottom.
• rot90 
– rotates.
• tril
– extracts the lower triangular part
• triu
– extracts the upper triangular part.
Some useful functions for matrix 
information
• size
– Give the dimension information of a matrix
• Length
– Give the number of elements of a vector
• find
– Find the index of non‐zero elements of matrix.
Multidimensional arrays
• MATLAB arrays can have more than two dimensions.
– e.g. >>a(:,:,1) = [1:2; 3:4]
>>a(:,:,2) = [5:6; 7:8]           
a(:,:,1) =
1     2
3     4
a(:,:,2) =
5     6
7     8
• It helps to think of the 3‐D array a as a series of ‘pages’, 
with a matrix on each page. The third dimension of a 
numbers the pages
Matlab functions on matrices
• The operations on each elements of the matrix 
could be different on the operations on
the matrices as a mathematical object
• Some built‐in matlab function operates on every 
element of the matrix, as you would expect
– e.g. >> sin([1,2;3,4])
ans =
0.8415    0.9093
0.1411   ‐0.7568
(cont’)
• However, some built‐in matlab function operates 
the matrix column‐wise
– e.g.  >> sum([1,2;3,4])
ans = 1 2
4     6 3    4

• If you are not sure whether a particular function 
operates column‐wise or element by element on 
matrices, you can always request help.
Basic matrix operators
• Matrix addition (element‐wise)
>> a=[1,2;3;4];
>> b=[5,6;0,1];
>> a+b
ans =
6     8
3     3
• Matrix subtraction (element wise)
>> a‐b
ans = 
‐4    ‐4
3     5
(cont’)
• Matrix multiplication (NOT element‐wise)
– In general

>> a*b
ans =
5     4
15    14
>> b*a
ans =
23    34
‐3    ‐4
How to do element‐wise multiplication
• Element‐wise multiplication vs matrix 
multiplication
n
( A·*B )i , j  ai , j bi , j vs ( A * B )i , j   ai ,k bk , j
k 1

>> a.* b
ans =
5    12
0    ‐4  
Matrix exponential
• matrix exponential (NOT element‐wise)

– A should be a square matrix
>> a^3
– If need element‐wise exponential
>> a.^3
Some other useful matrix function
• det
– determinant.
• eig
– eigenvalue decomposition.
• inv
– Inverse matrix
• svd
– singular value decomposition.

Você também pode gostar