mat lab tutor ail
TRANSCRIPT
-
8/13/2019 Mat Lab Tutor Ail
1/13
Introductory Matlab Tutorial
Mohamed Aslan
April 1, 2010
1 Introduction
Variables
To create a variable just use it on the left hand side of an equal sign:
1 x = 1 0 ;2 z = x 0 . 2 5 ;
To print the value of a variable, just type its name without the semi-colon:
1 z
Vectors
A vector is a matrix with either one row or one column. A vector is definedby placing a sequence of numbers within square braces.
1 % H o r i z o nt a l v e c t o r2 h = [3 1 0 ] ;3 % V e r t i c a l v e ct o r
4 v = [4 ; 5 ; 6 ] ;
Matrices
Creating a matrix is the same as a vector, separate the rows of a matrix usingsemicolons:
1 A = [ 1 2 3 ; 2 3 1 ; 1 10 1];
1
-
8/13/2019 Mat Lab Tutor Ail
2/13
To transpose a matrix:
1 B = A ;
Matrix multiplication:
1 A = [ 3 2 1 ; 2 0 1 ; 1 1 1 ] ;2 A = [ 1 6 3 ; 4 3 4 ; 1 3 2 ] ;3 C = A B;
Conditional If
Execute statements if condition is true.if expression1statements1elseif expression2statements2elsestatements3end
1 A = 5 ;2 B = 6 ;3 C = 0 ;4 i f A == B5 C = 1 ;6 e l s e i f A < B7 C = 2 ;8 e l s e9 C = 3 ;
10 en d
For Loop
The for loop executes statements for a number of times.for index = start:increment:endstatementsend
1 x = 0 ;2 f o r k = 1 : 103 x = x + k;4 en d
2
-
8/13/2019 Mat Lab Tutor Ail
3/13
Math Functions
Trigonometry
sin Sine
sind Sine in Degrees
asin Inverse sine
asind Inverse sine in Degrees
cos Cosine
cosd Cosine in Degrees
acos Inverse cosine
acosd Inverse cosine in Degrees
tan Tangent
tand Tangent in Degrees
atan Inverse tangent
atand Inverse tangent in Degrees
sec Secant
secd Secant in Degrees
asec Inverse secant
asecd Inverse secant in Degrees
csc Cosecant
cscd Cosecant in Degrees
acsc Inverse cosecant
acscd Inverse cosecant in Degrees
cot Cotangent
cotd Cotangent in Degrees
acot Inverse cotangent
3
-
8/13/2019 Mat Lab Tutor Ail
4/13
acotd Inverse cotangent in Degrees
1 % Fin d t he s i n e o f a n g le x = 0 . 9 r a di a n2 x = 0 . 9 ;3 y = s i n( x ) ;
Exponential
a n a to the power n
sqrt Square root
log10 Base 10 logarithm
exp Exponential (Natural)
log Natural logarithm (ln)
1 % Fin d th e s qu ar e r o o t o f x = 42 x = 4 ;3 y = s q r t( x ) ;
Rounding
round Round towards nearest integer
floor Round towards minus
ceil Round towards plus
1 x = 4 . 5 ;
2 y = round ( x ) ;
Other Functions
abs Absolute value
4
-
8/13/2019 Mat Lab Tutor Ail
5/13
Statistics Functions
Min
1 X = [ 5 5 1 2 4 4 ] ;2 % Find the minimum3 C = min ( X ) ;
Max
1 X = [ 5 5 1 2 4 4 ] ;2 % Find the maximum3 C = max( X ) ;
Mean, Average
1 X = [ 5 5 1 2 4 4 ] ;2 % F in d th e mean o f a v e ct o r3 M = mean ( X ) ;4 % F in d t he mean o f a ma t r ix5 A = [ 1 2 3 ; 3 3 6 ; 4 6 8 ; 4 7 7 ] ;6 Z = mean ( A ) ;
Median
1 X = [ 5 5 1 2 4 4 ] ;2 % F in d t h e m ed ia n3 M = median ( X ) ;
Mode
1 X = [ 5 5 1 2 4 4 ] ;2 % F in d t h e mode3 M = mode ( X ) ;
5
-
8/13/2019 Mat Lab Tutor Ail
6/13
Standard Deviation
1 X = [ 5 5 1 2 4 4 ] ;2 % F in d t h e s t an d ar d d e v i a ti o n3 S = s t d( X ) ;
2 Graphs
Plotting
1 X = [1 2 3 4 5 ] ;2 Y = [1 4 9 16 2 5 ];3 % P lo t X a g ai n s t Y f o r f u n ct i o n Y = X24 p l o t( X, Y ) ;5 % Try p l o t (X , Y , ) and p l o t (X , Y , r . )6 % S et t h e gr ap h s t i t l e7 t i t l e( Y=X2 ) ;8 % Se t the l a b el s f o r Xa x i s and Ya x i s9 x l a b e l( X ) ;
10 y l a b e l( Y ) ;
Bar Chart
1 z = [ 1 2 3 4 6 4 3 4 5 ] ;2 ba r ( z ) ;3 x l a b e l( I n d e x ) ;4 y l a b e l( Value ) ;
3 Image Processing
Read Image
1 % L oa d t h e i m ag e2 im = i m r e a d ( f i l e n a m e ) ;
Display Image
6
-
8/13/2019 Mat Lab Tutor Ail
7/13
1 % D i s p l ay t h e i ma ge2 i m s h o w ( im ) ;
Get Size of Image
1 % Get h e i g h t and wi dt h o f t h e i ma ge2 [ y, x] = s i z e( im ) ;
Complement Image
1 % Complement the image2 n e w _ i m g = i m c o m p l e m e n t ( im g ) ;
RGB to Grayscale
1 % C o n ve r t RGB i ma g e t o g r a y s c a l e i m ag e2 i m _ g r a y = r g b 2 g r a y ( i m _ r g b ) ;
Grayscale to BW
1 % C on ve rt g r a y s c a l e i m ag e t o b l a c k and w h it e i ma ge w i th t h r e s h o l d o f 0 . 5
2 i m _ b w = i m 2 b w ( im_gray , 0 . 5 ) ;3 % To c a l c u l a t e t he t h r es h o ld a u t om a t ic a ll y a nd t he n c o nv e rt t o b l a ck
a nd w h i t e i ma g e4 th r = g r a y t h r e s h ( i m _ g r a y ) ;5 i m _ b w = i m 2 b w ( im_gray , th r ) ;
Save Image
1 % Save image to a BMP f i l e2 i m w r i t e ( im , f i l e na me .bmp , bmp ) ;3 % Save image t o a JPEG f i l e4 i m w r i t e ( im , f i l e na me . j pg , jpg ) ;
7
-
8/13/2019 Mat Lab Tutor Ail
8/13
Crop Image
1 % C r ea t e s an i n t e r a c t i v e C rop Ima ge t o o l a s s o c i a t e d w i th t h e im ag e2 im = i m c r o p3 % Cro ps t he i ma ge and s p e c i f i e s t he s i z e a nd p o s i t i o n o f t he c ro p
r e c t a n g l e4 i m _ c r o p = i m c r o p ( im , [x y w h ] ) ;
Subtract Images
1 % S u b t r a c t s on e i ma ge fr om a no t h e r2 im = i m s u b t r a c t ( im 1, im 2 ) ;
Entropy of Grayscale Images
1 % C a l c u l a t e s t h e e n tr o py o f g r a y s c a l e im ag e2 j = e n t r o p y ( im ) ;
Image Histograms
1 % D i s pl a y s th e hi st o gr a m o f i ma ge2 i m h i s t ( im ) ;
4 Video Processing
Acquire Video
1 % S e l e c t v id eo d e vi c e2 ca m = v i d e o i n p u t ( winvi deo , 3 ) ;3 % D i sp la y t he l i v e v id eo4 p r e v i e w ( ca m ) ;5 % Tak e t h e s n a ps h o t6 im = g e t s n a p s h o t ( ca m ) ;
8
-
8/13/2019 Mat Lab Tutor Ail
9/13
5 Audio Processing
Acquire Audio
1 r e c o r d e r = a u d i o r e c o r d e r;2 % R ec or d v o i c e f o r 5 s e co n ds3 r e c o r d b l o c k i n g ( recorder , 5 ) ;4 % P la y t h e r e c o r d i n g5 play ( r e c o r d e r ) ;6 % S t o re so un d d at a in a r r ay7 sn d = g e t a u d i o d a t a ( r e c o r d e r ) ;8 % P l o t a u d i o w av ef or m9 p l o t( sn d ) ;
Load Audio (from Wav file)
1 sn d = wavread ( song .wav ) ;2 % P l o t a u d i o w av ef or m3 p l o t( sn d ) ;
6 2D-Filters
average: Averaging filter
gaussian: Gaussian lowpass filter
laplacian: Approximates the two-dimensional Laplacian operator
motion: Approximates the linear motion of a camera
sobel: Sobel horizontal edge-emphasizing filter
unsharp: Unsharp contrast enhancement filter
Average
1 % C hoos e the f i l t e r2 f = f s p e c i a l ( ave rag e , [ 3 , 3 ] ) ;3 % P a s s t h r o u gh a v e r a g e f i l t e r4 f i l t e r e d _ o b j = i m f i l t e r ( ob j, f ) ;
9
-
8/13/2019 Mat Lab Tutor Ail
10/13
Gaussian
1 % Cho ose t he f i l t e r , w it h s ig ma = 0 . 52 f = f s p e c i a l ( g a u s s i a n , [ 3 , 3 ] , 0 . 5 ) ;3 % P a s s t h r o u gh g a u s s i a n f i l t e r4 f i l t e r e d _ o b j = i m f i l t e r ( ob j, f ) ;
Laplacian
1 % Cho ose t he f i l t e r , w it h a lp ha = 0 . 2 ( b et wee n 0 . 0 t o 1 . 0 )2 f = f s p e c i a l ( l a p l a c i a n , 0 . 2 ) ;3 % P a ss t h ro u gh l a p l a c i a n f i l t e r4 f i l t e r e d _ o b j = i m f i l t e r ( ob j, f ) ;
Motion
1 % Cho ose t he f i l t e r , w it h l e n = 2 p i x e l s and t h et a = 02 f = f s p e c i a l ( motion , 2 , 0 ) ;3 % P a s s t h r o u gh m o ti o n f i l t e r4 f i l t e r e d _ o b j = i m f i l t e r ( ob j, f ) ;
Sobel
1 % C hoos e the f i l t e r2 f = f s p e c i a l ( s o b e l ) ;3 % P a ss t h ro u gh s o b e l f i l t e r4 f i l t e r e d _ o b j = i m f i l t e r ( ob j, f ) ;
Unsharp
1 % Cho ose t he f i l t e r , w it h a lp ha = 0 . 2 ( b et wee n 0 . 0 t o 1 . 0 )2 f = f s p e c i a l ( uns harp , 0 . 2 ) ;3 % P a s s t h r o u gh u n sh a r p f i l t e r4 f i l t e r e d _ o b j = i m f i l t e r ( ob j, f ) ;
10
-
8/13/2019 Mat Lab Tutor Ail
11/13
7 Fourier Analysis
Discrete Fourier Transform
1 X = [ 1 2 3 4 5 6 ] ;2 % Co mp ut es t h e d i s c r e t e F o u r i e r t r a n s f o r m (DFT) o f v e c t o r X3 Y = f f t( X ) ;
Inverse Discrete Fourier Transform
1 X = [ 1 2 3 4 5 6 ] ;2 % Co mp ut es t h e d i s c r e t e F o u r i e r t r a n s f o r m (DFT) o f v e c t o r X3 Y = f f t( X ) ;4 % Computes t h e i n v e r s e d i s c r e t e F o u r ie r t r a n s fo r m ( IDFT ) o f v e c t o r Y5 X = i f f t( Y ) ;
2-D Discrete Fourier Transform
1 X = [ 1 2 3 ; 4 5 6 ] ;
2 % C ompute s the 2D d i s c r e t e F o u r ie r t r a ns f or m (DFT) o f m at ri x X3 Y = f f t 2( X ) ;
2-D Inverse Discrete Fourier Transform
1 X = [ 1 2 3 4 5 6 ] ;2 % C ompute s the 2D d i s c r e t e F o u r ie r t r a ns f or m (DFT) o f m at ri x X3 Y = f f t 2( X ) ;4 % Co mp ut es t h e i n v e r s e 2D d i s c r e t e F o u r ie r t r a ns f or m ( IDFT ) o f m at ri x
Y5 X = i f f t 2( Y ) ;
Zero Shift
1 % S h i f t z er of r e q u e n c y c o mp on en t t o c e n t e r o f s p ec t ru m2 Z = f f t s h i f t( Y ) ;
11
-
8/13/2019 Mat Lab Tutor Ail
12/13
8 Wavelet Analysis
Single-level discrete 1-D wavelet transform
1 % Computes 1 l e v e l d i s c r et e ha ar 1D w a v e l et t r a n s fo r m2 [ A, D] = dw t ( X, h a a r ) ;3 % Computes 1 l e v e l d i s c r et e db1 1D w a v e l et t r a n s f or m4 [ A, D] = dw t ( X, db1 ) ;5 % Computes 1 l e v e l d i s c r et e db2 1D w a v e l et t r a n s f or m6 [ A, D] = dw t ( X, db2 ) ;7 % Computes 1 l e v e l d i s c r et e db4 1D w a v e l et t r a n s f or m8 [ A, D] = dw t ( X, db4 ) ;9 % Computes 1 l e v e l d i s c r e t e c o i f4 1D w a v e l et t r a n s f or m
10 [ A, D] = dw t ( X, c o i f 4 ) ;
Single-level inverse discrete 1-D wavelet transform
1 % Computes 1 l e v e l d i s c r et e ha ar 1D w a v e l et t r a n s fo r m2 [ A, D] = dw t ( X, h a a r ) ;3 % Computes 1 l e v e l i n ve r se d i s c r et e ha ar 1D w a v e l et t r a n s fo r m4 X = idwt ( A, D, h a a r ) ;
Single-level discrete 2-D wavelet transform
1 % Computes 1 l e v e l d i s c r et e ha ar 2D w a v e l et t r a n s fo r m2 [ A, H, V, D] = dwt2 ( X, h a a r ) ;
Single-level inverse discrete 2-D wavelet transform
1 % Computes 1 l e v e l d i s c r et e ha ar 2D w a v e l et t r a n s fo r m2 [ A, H, V, D] = dwt2 ( X, h a a r ) ;3 % Computes 1 l e v e l i n ve r se d i s c r et e ha ar 2D w a v e l et t r a n s fo r m4 X = i d w t 2 ( A, H, V D, h a a r ) ;
9 Artificial Neural Networks
Feed-Forward Neural Networks
Example: XOR gate
12
-
8/13/2019 Mat Lab Tutor Ail
13/13
1 % C r e at e f e e df o r w a r d n e tw o r k2 % Parame te rs :3 % 1 Max a nd min o f t h e i n pu t v a l u e s ( 2 i n p u ts )4 % 2 Number o f l a y e r s 2 l a y e r s , 1 s t ( h d id de n ) h as 2 n eu ro ns , 2 nd (
o u tp u t ) h as 1 n e ur o n5 % 3 A ct i va t io n f u n c ti o n s f o r e ac h l a ye r , s ig mo id f u n ct i o n i s u se d6 ne t = n e w f f( [ 0 1 ; 0 1 ] , [ 2 1 ] , { l o g s i g , l o g s i g }) ;78 % I np ut i s i n t he c ol umn s o f th e ma tr ix9 % 1 1 0 0
10 % 1 0 1 011 % The 1 s t i np ut i s 1 1 , t he 2 nd i s 1 0 , t he 3 r d i s 0 1 and t h e 4 th i s
0 012 i n p u t = [ 1 1 0 0 ; 1 0 1 0 ] ;1314 % T a rg e t m at r ix ( e x p e ct e d o u tp u t )
15 t a r g e t = [0 1 1 0 ] ;1617 % T r ai n t h e n et wo r k18 ne t = t r a i n ( ne t, input, t a r g e t ) ;1920 % S i m ul a t e t h e n et wo r k21 % T es t t h e n et wo rk w i th i n pu t a s i n pu t a nd s e e i f o ut pu t i s t h e same
a s t a r g e t22 o u t p u t = si m ( ne t, i n p u t)
13