1
INTRODUCTION TO MATLAB AND LINEAR ALGEBRA
MATLAB (MATrix LABoratory)
2
Originally developed to provide easy access to matrix, LINPACK (linear system package) in the 1970s Numerical computation Data visualization Applications
Image
processing Control systems Bioinformatics
Screenshot
3
Editor
(Edit m-files)
Current Folder
Workspace
(Current Variables)
Command Window
Variables and vectors
4
Vector Operations
5
Colon (:) can be used to create vectors
start:end
>> a = 1:4 a= 1 2
start:step:end
>> a = 1:0.5:4 a= 1.0000 1.5000
2.0000
2.5000
3.0000
3.5000
4.0000
Vector Operations
6
Matrix
7
rows
columns
() indicates address [] indicates numerical value
Matrix operations
8
Delete row (column)
3 6
>> a(3,:) = [] a= 1 2 4 5 >> d (:,[1 3]) = [] d= 3 6 9
Matrix operations
9
Matrix operation
10
Concatenation
>> a = [6 2 4;1 3 4] a= 6 2 4 1 3 4 >> b = [8 7 5] b= 8 7 5 >> c = [a;b] c= 6 2 4 1 3 4 8 7 5
Matrix Operations
11
Special Matrices
12
Matrix Operations
13
diag
diag(matrix) >> a = [1 2 3;4 5 6;7 8 9] a= 1 2 3 4 5 6 7 8 9 >> diag(a) ans = 1 5 9 diag(vector) >> a = diag([2 -1 3]) a= 2 0 0 0 -1 0 0 0 3
Matrix operations
14
Simple operations
Addition + Subtraction Multiplication *
>> a = [1 2;2 3] a= 1 2 2 3 >> b = [1 6;3 2] b= 1 6 3 2 >> a+b ans = 2 8 5 5
>> c = [1 2 3;4 5 6] c= 1 2 3 4 5 6 >> d = [5 3 1] d= 5 3 1 >> c*d ??? Error using ==> mtimes Inner matrix dimensions must agree. >> c*d' ans = 14 41
Matrix operations
15
Element-wise operations
Multiplication .*
Division Power
./ .^
>> a = [2 -1 3] a= 2 -1 3 >> b = [0 3 1] b= 0 3 1 >> a.*b ans = 0 -3 3 >> a.^b ans = 1 -1 3
Solving Linear Equations
16
Solving Ax = b (Amxn , bmx1)
rref
>> A = [1 2 3; -1 3 4; 4 5 6] A= 1 2 3 -1 3 4 4 5 6 >> b = [2; 4; 5] b= 2 4 5 >> rref([A b]) ans = 1.0000 0 0 0 1.0000 0 0 0 1.0000
-0.3333 1.6667 -0.3333
Solving Linear Equations
17
Solving Ax = b (Amxn , bmx1)
x = A\b
>> x = a\b x= -0.3333 1.6667 -0.3333
Matrix Inverse
18
inv(A)
>> A=[1 2 3;4 5 6;7 8 10]; >> inv(A) ans = -0.6667 -1.3333 1.0000 -0.6667 3.6667 -2.0000 1.0000 -2.0000 1.0000
>> A2 = [A eye(3)] A2 = 1 2 3 1 0 0 4 5 6 0 1 0 7 8 10 0 0 1 >> A3 = rref(A2) A3 = 1.0000 0 0 -0.6667 -1.3333 1.0000 0 1.0000 0 -0.6667 3.6667 -2.0000 0 0 1.0000 1.0000 -2.0000 1.0000 >> A4=A3(:,4:6) A4 = -0.6667 -1.3333 1.0000 -0.6667 3.6667 -2.0000 1.0000 -2.0000 1.0000
rref([A I]) = [I A-1]
Plot
19
Plot
20
Plot a set of linear equations
21
1 Solution
No Solution
Solutions
M-File vs. Command Line
22
Coding in Command Window
short programs Commands must be re-entered each time you run a simulation
Coding with .m-files
long programs Allows users to save all the commands written in the .m-files
Function
23
Function
function [out1 out2 out3] = Myfunction(in1, in2) . . end if condition statemeant 1 else statemeant 2 end for i=1:n statement end
IF
For Loop
Example
24
function plotIntersection(a1,b1,a2,b2) x = -10:0.1:10; plot(a1*x+b1); hold on; plot(a2*x+b2,'r'); end
Other Useful functions
25
rref(a): reduce row echelon (chapter 1) lu(a): LU decomposition of matrix a (chapter 2) det(a): determinant of a matrix (chapter 3) rank(a): calculate rank of matrix a (chapter 4) null(a): gives the null space of matrix (chapter 4) norm(a): norm of vector or matrix a (chapter 5) orth(a): orthogonal bases of matrix a (chapter 5) eig(a): eigenvalue decomposition of matrix a (chapter 7)
Applications (Image processing)
26
img = imread(filepath) : reads an image Imshow(img) : shows an image
Submatrix
27
>> size(img) ans = 511 383 >>imshow(img(100:200,200:300))
Transpose
28
>> Imshow(img')
Assignment
29
>> img(100:200,:) = 255; >> imshow(img)
Delete columns
30
>> img(:,150:300) = []; >> imshow(img);