Documente Academic
Documente Profesional
Documente Cultură
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
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
1 2 3
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
Rounding round Round towards nearest integer oor Round towards minus ceil Round towards plus
1 2
x = 4.5; y = round ( x ) ;
Statistics Functions
Min
1 2 3
Max
1 2 3
Mean, Average
1 2 3 4 5 6
X % M % A Z
Median
1 2 3
Mode
1 2 3
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 ) ;
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 ) ;
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 ) ;
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
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
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