Sunteți pe pagina 1din 13

Introductory Matlab Tutorial

Mohamed Aslan April 1, 2010

Introduction

Variables
To create a variable just use it on the left hand side of an equal sign:
1 2 x = 10; z = x 0.25;

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 dened by placing a sequence of numbers within square braces.
1 2 3 4 % h % v Horizontal vector = [3 1 0 ] ; Vertical vector = [4 ; 5 ; 6 ] ;

Matrices
Creating a matrix is the same as a vector, separate the rows of a matrix using semicolons :
1 A = [ 1 2 3 ; 2 3 1 ; 1 10 1 ] ;

To transpose a matrix:
1 B = A ;

Matrix multiplication:
1 2 3 A = [3 2 1 ; 2 0 1 ; 1 1 1 ] ; A = [1 6 3 ; 4 3 4 ; 1 3 2 ] ; C = A B;

Conditional If
Execute statements if condition is true. if expression1 statements1 elseif expression2 statements2 else statements3 end
1 2 3 4 5 6 7 8 9 10 A = 5; B = 6; C = 0; i f A == B C = 1; elseif A < B C = 2; else C = 3; end

For Loop
The for loop executes statements for a number of times. for index = start:increment:end statements end

1 2 3 4

x = 0; for k = 1:10 x = x + k; end

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

acotd Inverse cotangent in Degrees

1 2 3

% Find t h e s i n e o f a n g l e x = 0 . 9 r a d i a n x = 0.9; y = sin (x) ;

Exponential a n a to the power n sqrt Square root log10 Base 10 logarithm exp Exponential (Natural) log Natural logarithm (ln)

1 2 3

% Find t h e s q u a r e r o o t o f x = 4 x = 4; y = sqrt (x) ;

Rounding round Round towards nearest integer oor Round towards minus ceil Round towards plus

1 2

x = 4.5; y = round ( x ) ;

Other Functions abs Absolute value

Statistics Functions
Min

1 2 3

X = [5 5 1 2 4 4 ] ; % Find t h e minimum C = min ( X ) ;

Max

1 2 3

X = [5 5 1 2 4 4 ] ; % Find t h e maximum C = max( X ) ;

Mean, Average

1 2 3 4 5 6

X % M % A Z

= [5 5 1 2 4 4 ] ; Find t h e mean o f a v e c t o r = mean ( X ) ; Find t h e mean o f a m a t r i x = [1 2 3; 3 3 6; 4 6 8; 4 7 7 ] ; = mean ( A ) ;

Median

1 2 3

X = [5 5 1 2 4 4 ] ; % Find t h e median M = median ( X ) ;

Mode

1 2 3

X = [5 5 1 2 4 4 ] ; % Find t h e mode M = mode ( X ) ;

Standard Deviation

1 2 3

X = [5 5 1 2 4 4 ] ; % Find t h e s t a n d a r d d e v i a t i o n S = std ( X ) ;

Graphs

Plotting
1 2 3 4 5 6 7 8 9 10 X = [1 2 3 4 5 ] ; Y = [ 1 4 9 16 2 5 ] ; % P l o t X a g a i n s t Y f o r f u n c t i o n Y = X2 plot (X , Y) ; % Try p l o t (X, Y, ) and p l o t (X, Y, r . ) % S e t t h e graph s t i t l e t i t l e ( Y=X2 ) ; % S e t t h e l a b e l s f o r X a x i s and Y a x i s x l a b e l ( X ) ; y l a b e l ( Y ) ;

Bar Chart
1 2 3 4 z = [1 2 3 4 6 4 3 4 5 ] ; bar ( z ) ; x l a b e l ( Index ) ; y l a b e l ( Value ) ;

Image Processing

Read Image
1 2 % Load t h e image im = imread ( filename ) ;

Display Image

1 2

% D i s p l a y t h e image imshow ( im ) ;

Get Size of Image


1 2 % Get h e i g h t and width o f t h e image [ y , x ] = s i z e ( im ) ;

Complement Image
1 2 % Complement t h e image new_img = imcomplement ( img ) ;

RGB to Grayscale
1 2 % Convert RGB image t o g r a y s c a l e image im_gray = rgb2gray ( im_rgb ) ;

Grayscale to BW
1 2 3 4 5 % Convert g r a y s c a l e image t o b l a c k and w h i t e image with t h r e s h o l d o f 0.5 im_bw = im2bw ( im_gray , 0 . 5 ) ; % To c a l c u l a t e t h e t h r e s h o l d a u t o m a t i c a l l y and then c o n v e r t t o b l a c k and w h i t e image thr = graythresh ( im_gray ) ; im_bw = im2bw ( im_gray , thr ) ;

Save Image
1 2 3 4 % Save image imwrite ( im , % Save image imwrite ( im , t o a BMP f i l e f i l e n a m e . bmp , bmp ) ; t o a JPEG f i l e filename . jpg , jpg ) ;

Crop Image
1 2 3 4 % C r e a t e s an i n t e r a c t i v e Crop Image t o o l a s s o c i a t e d with t h e image im = imcrop % Crops t h e image and s p e c i f i e s t h e s i z e and p o s i t i o n o f t h e c r o p rectangle im_crop = imcrop ( im , [ x y w h ] ) ;

Subtract Images
1 2 % S u b t r a c t s one image from a n o t h e r im = imsubtract ( im1 , im2 ) ;

Entropy of Grayscale Images


1 2 % C a l c u l a t e s t h e e n t r o p y o f g r a y s c a l e image j = entropy ( im ) ;

Image Histograms
1 2 % D i s p l a y s t h e h i s t o g r a m o f image imhist ( im ) ;

Video Processing

Acquire Video
1 2 3 4 5 6 % Select video device cam = videoinput ( w i n v i d e o , 3 ) ; % Display the l i v e video preview ( cam ) ; % Take t h e s n a p s h o t im = getsnapshot ( cam ) ;

Audio Processing

Acquire Audio
1 2 3 4 5 6 7 8 9 recorder = audiorecorder ; % Record v o i c e f o r 5 s e c o n d s recor dblocki ng ( recorder , 5 ) ; % Play t h e r e c o r d i n g play ( recorder ) ; % S t o r e sound data i n a r r a y snd = getaudiodata ( recorder ) ; % P l o t a u d i o waveform p l o t ( snd ) ;

Load Audio (from Wav le)


1 2 3 snd = wavread ( song . wav ) ; % P l o t a u d i o waveform p l o t ( snd ) ;

2D-Filters
average: Averaging lter gaussian: Gaussian lowpass lter laplacian: Approximates the two-dimensional Laplacian operator motion: Approximates the linear motion of a camera sobel: Sobel horizontal edge-emphasizing lter unsharp: Unsharp contrast enhancement lter

Average
1 2 3 4 % Choose t h e f i l t e r f = fspecial ( a v e r a g e , [ 3 , 3 ] ) ; % Pass t h r o u g h a v e r a g e f i l t e r filtered_obj = imfilter ( obj , f ) ;

Gaussian
1 2 3 4 % Choose t h e f i l t e r , with sigma = 0 . 5 f = fspecial ( g a u s s i a n , [ 3 , 3 ] , 0 . 5 ) ; % Pass t h r o u g h g a u s s i a n f i l t e r filtered_obj = imfilter ( obj , f ) ;

Laplacian
1 2 3 4 % Choose t h e f i l t e r , with a l p h a = 0 . 2 ( between 0 . 0 t o 1 . 0 ) f = fspecial ( l a p l a c i a n , 0 . 2 ) ; % Pass t h r o u g h l a p l a c i a n f i l t e r filtered_obj = imfilter ( obj , f ) ;

Motion
1 2 3 4 % Choose t h e f i l t e r , with l e n = 2 p i x e l s and t h e t a = 0 f = fspecial ( motion , 2 , 0 ) ; % Pass t h r o u g h motion f i l t e r filtered_obj = imfilter ( obj , f ) ;

Sobel
1 2 3 4 % Choose t h e f i l t e r f = fspecial ( s o b e l ) ; % Pass t h r o u g h s o b e l f i l t e r filtered_obj = imfilter ( obj , f ) ;

Unsharp
1 2 3 4 % Choose t h e f i l t e r , with a l p h a = 0 . 2 ( between 0 . 0 t o 1 . 0 ) f = fspecial ( unsharp , 0 . 2 ) ; % Pass t h r o u g h unsharp f i l t e r filtered_obj = imfilter ( obj , f ) ;

10

Fourier Analysis

Discrete Fourier Transform


1 2 3 X = [1 2 3 4 5 6 ] ; % Computes 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 X Y = fft (X) ;

Inverse Discrete Fourier Transform


1 2 3 4 5 X % Y % X = [1 2 3 4 5 6 ] ; Computes 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 X = fft (X) ; Computes t h e i n v e r s 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 (IDFT) o f v e c t o r Y = i f f t (Y) ;

2-D Discrete Fourier Transform


1 2 3 X = [1 2 3 ; 4 5 6 ] ; % Computes t h e 2D 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 m a t r i x X Y = fft2 (X) ;

2-D Inverse Discrete Fourier Transform


1 2 3 4 5 X % Y % = [1 2 3 4 5 6 ] ; Computes t h e 2D 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 m a t r i x X = fft2 (X) ; Computes t h e i n v e r s e 2D d i s c r e t e F o u r i e r t r a n s f o r m (IDFT) o f m a t r i x Y X = i f f t 2 (Y) ;

Zero Shift
1 2 % S h i f t z e r o f r e q u e n c y component t o c e n t e r o f spectrum Z = f f t s h i f t (Y) ;

11

Wavelet Analysis

Single-level discrete 1-D wavelet transform


1 2 3 4 5 6 7 8 9 10 % Computes 1 l e v e l d i s c r e t e [ A , D ] = dwt ( X , haar ) ; % Computes 1 l e v e l d i s c r e t e [ A , D ] = dwt ( X , db1 ) ; % Computes 1 l e v e l d i s c r e t e [ A , D ] = dwt ( X , db2 ) ; % Computes 1 l e v e l d i s c r e t e [ A , D ] = dwt ( X , db4 ) ; % Computes 1 l e v e l d i s c r e t e [ A , D ] = dwt ( X , c o i f 4 ) ; haar 1D w a v e l e t t r a n s f o r m db1 1D w a v e l e t t r a n s f o r m db2 1D w a v e l e t t r a n s f o r m db4 1D w a v e l e t t r a n s f o r m c o i f 4 1D w a v e l e t t r a n s f o r m

Single-level inverse discrete 1-D wavelet transform


1 2 3 4 % Computes 1 l e v e l d i s c r e t e haar 1D w a v e l e t t r a n s f o r m [ A , D ] = dwt ( X , haar ) ; % Computes 1 l e v e l i n v e r s e d i s c r e t e haar 1D w a v e l e t t r a n s f o r m X = idwt ( A , D , haar ) ;

Single-level discrete 2-D wavelet transform


1 2 % Computes 1 l e v e l d i s c r e t e haar 2D w a v e l e t t r a n s f o r m [ A , H , V , D ] = dwt2 ( X , haar ) ;

Single-level inverse discrete 2-D wavelet transform


1 2 3 4 % Computes 1 l e v e l d i s c r e t e haar 2D w a v e l e t t r a n s f o r m [ A , H , V , D ] = dwt2 ( X , haar ) ; % Computes 1 l e v e l i n v e r s e d i s c r e t e haar 2D w a v e l e t t r a n s f o r m X = idwt2 ( A , H , V D , haar ) ;

Articial Neural Networks

Feed-Forward Neural Networks


Example: XOR gate 12

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

C r e a t e f e e d f o r w a r d network P a ra m e t e r s : 1 Max and min o f t h e i n p u t v a l u e s ( 2 i n p u t s ) 2 Number o f l a y e r s 2 l a y e r s , 1 s t ( hdidden ) has 2 neurons , 2nd ( ou tp ut ) has 1 neuron % 3 A c t i v a t i o n f u n c t i o n s f o r each l a y e r , s i g m o i d f u n c t i o n i s used net = newff ( [ 0 1 ; 0 1 ] , [ 2 1 ] , { l o g s i g , l o g s i g } ) ; % % % % % % % % I n p u t i s i n t h e columns o f t h e m a t r i x 1 1 0 0 1 0 1 0 The 1 s t i n p u t i s 1 1 , t h e 2nd i s 1 0 , t h e 3 rd i s 0 1 and t h e 4 th i s 0 0 input = [1 1 0 0 ; 1 0 1 0 ] ;

% Ta rge t m a t r i x ( e x p e c t e d o ut pu t ) target = [ 0 1 1 0 ] ; % Train t h e network net = train ( net , i n p u t , target ) ; % S i m u l a t e t h e network % Test t h e network with i n p u t a s i n p u t and s e e i f ou tp ut i s t h e same as t a r g e t output = sim ( net , i n p u t )

13

S-ar putea să vă placă și