Lab 3: Matlab Tutorial
Lab 3: Matlab Tutorial
Zhuo Wang
Matrix manipulations
Visualization of data
Implementation of algorithms
Maoying Wu SysBio
MATLAB: Layouts
Current Folder
Editor
Command Window
Workspace
Command History
Maoying Wu SysBio
Using help or doc
help
help command
help on toolbar
doc command
doc query
Maoying Wu SysBio
Matlab classes: primitives
Maoying Wu SysBio
MATLAB variables
Basic operations
sum, subtract, multiply, divide
Maoying Wu SysBio
Scalars, Vectors and Arrays
Maoying Wu SysBio
Getting Started: Assignment
1 2 1
A= ,b = , Ax = b, x = A−1 b
2 1 2×2
2 2×1
a = [ 1 2; 2 1 ];
a
b = [1;2];
x = a\b
x = inv ( a ) * b
1×1 2×2
A. ∗ A0 = (A is symmetric)
2×2 1×1
a .* a
1×1+2×2 1×2+2×1
A∗A=
2×1+1×2 2×2+1×1
a*a
Maoying Wu SysBio
Exercise
1
Let π = be the initial probability for two states, and
0
0.8 0.1
T = be the transition probability matrix between
0.2 0.9
states 1 and 2.
pi = [1; 0];
t = [0.8 0.1; 0.2 0.9];
for i =1:20 , pi = t * pi , end
Maoying Wu SysBio
Vector: initialization
Maoying Wu SysBio
Vector: Sorting
Maoying Wu SysBio
Vector: norm
n
xip )1/p
X
||x||p = (
i=1
Maoying Wu SysBio
Vector: Histogram
x = rand (10 , 1) ;
x = x (:) ;
% histogram : the mid of bin and the count in each bin
[ count , mid ] = hist ( x ) ;
% how to get the bin , 10 bins
y = linspace ( min ( x ) , max ( x ) , 11)
all ( y (1:10) + ( y (2) -y (1) ) /2 == mid )
% count
count2 = [];
for i =1:10 , count2 ( i ) = sum (x >= y ( i ) & x <= y ( i +1) ) ; end
% are these two equal ?
all ( count == count2 )
Maoying Wu SysBio
Special double array
ones(n, m, p)
zeros(n, m, p, q)
rand(n, m)
randn(n, m)
cell(n, m)
Maoying Wu SysBio
Random array
s = rng ;
A = randn (5) ;
rng ( s )
B = randn (5) ;
A == B
Maoying Wu SysBio
The size command
Examples
A = rand (5 ,6 ,8) ;
B = size ( A ) ;
[ a1 , a2 , a3 , a4 ] = size ( A ) ;
[ a1 , a2 ] = size ( A ) ;
size (A , 2)
size (A , 6)
Maoying Wu SysBio
The class command
Examples
Maoying Wu SysBio
Struct array
Maoying Wu SysBio
File: opening, reading and writing
if ( in_file == -1)
error ( ’ oops , file can ’t be read ! ’) ;
end
end
Maoying Wu SysBio
fprintf
%s: a string
%c: a single character
%d: a whole number
%f: a floating point number
\n: a newline
\t: a tab
\\: a slash
%%: a percent sign
Maoying Wu SysBio
Flow control
for loops
while loops
if/elseif/else conditionals
switch/case/otherwise
try/catch
Do NOT forget the end at the end.
Maoying Wu SysBio
while-loop and for-loop
Listing 1: while-loop
while < condition >
if < something - bad - happens >
break
else
% do something useful
end
% do more things
end
Listing 2: for-loop
for < condition >
if < something - bad - happens >
next
elseif
% do something useful
else
% do something useful
end
% do more things
end
Maoying Wu SysBio
Graphics in MATLAB
t = 0:.3:10;
y = sin ( t ) ;
plot (t , y ) ;
Maoying Wu SysBio
3D graphics in MATLAB
Maoying Wu SysBio
Definition of Functions
N = sum ( ind ) ;
Z = zeros (M , N ) ;
for i =1: M
counter = 0;
for j =1: length ( ind )
if ( ind ( j ) )
counter = counter + 1;
Z (i , counter ) = letter2number ( align_full ( i ) .
Sequence ( j ) ) ;
end
end
end
Maoying Wu SysBio
Functions of the MATLAB Optimization
Maoying Wu SysBio
Linear Programming
Linear programming problem
min f (x) = c T x
s.t.
Ax ≤ a
Bx = b
lb ≤ x ≤ ub;
Maoying Wu SysBio
Algorithms under linprog
Maoying Wu SysBio
Linear programming: Example 1
c = [ -2 , -3] ’;
A = [1 ,2;2 ,1;0 ,1];
a = [8 ,10 ,3] ’;
options = optimset ( ’ linprog ’) ;
options = optimset ( ’ LargeScale ’ , ’ off ’) ;
xsol = linprog (c , A , a , [] , [] , [0 ,0] ’ , [] , [] , options )
Maoying Wu SysBio
Linear programming: Exercise
1 1 1 1 1 1 10
(A|a) =
5 0 −3 0 1 0 15
1 2 3 0 0 0 5
0 1 2 3 0 0 7
max cT x (B|b) =
0 0 1 2 3 0 8
s.t. 0 0 0 1 2 3 8
Ax =a
3 0 0 0 −2 1 5
(D|d) =
Bx ≥b 0 4 0 −2 0 3 7
Dx ≤d
−2 7 1
lb ≤ x ≤ ub 0 2 −2
−1 2 3
(lb|ub|c) =
−1 3 −4
−5 4 5
1 10 −6
Maoying Wu SysBio
Linear programming: solution.m
Maoying Wu SysBio