An Introduction to An Introduction to MatlabMatlabFor CSE 802: Pattern RecognitionFor CSE 802: Pattern [email protected]@cse.msu.eduStart Start MatlabMatlab►►You can access it from CSE lab but itYou can access it from CSE lab but it’’s more s more easy to go to engineering lab and use easy to go to engineering lab and use MatlabMatlabthere.there.►►Machines in engineering buildingMachines in engineering building’’s labs.s labs.StartStart-->All Programs>All Programs-->>MatlabMatlab7.0.7.0.►►License issues, especially for some toolbox.License issues, especially for some toolbox.Exit Exit MatlabMatlabif you do not use it.if you do not use it.TopicsTopics►►Data structure of Data structure of MatlabMatlab..►►Some useful Some useful MatlabMatlabfunctions for this course.functions for this course.►►Plotting of data.Plotting of data.►►Two examples:Two examples:Plotting of multivariate Gaussian data.Plotting of multivariate Gaussian data.PCA: compute PCA and plot the data of reduced PCA: compute PCA and plot the data of reduced dimensionality.dimensionality.Scalars, Vectors and MatricesScalars, Vectors and Matrices►►Scalar: Scalar: Just a number: a = 1; b = 3;Just a number: a = 1; b = 3;►►Vector:Vector:Column vector: a = [1; 2; 3; 4].Column vector: a = [1; 2; 3; 4].Row vector: b = [1 2 3 4].Row vector: b = [1 2 3 4].Transpose: a = bTranspose: a = b’’;;►►Matrix:Matrix:A = [1 2 3; 4 5 6; 7 8 9];A = [1 2 3; 4 5 6; 7 8 9];Access Elements in MatricesAccess Elements in Matrices►►Access a single element.Access a single element.A[rowA[rowindex, index, columcolumindex]index]A[1,3] = 3;A[1,3] = 3;►►Access a subAccess a sub--matrix.matrix.Extract out part of rows: B = A[1:2, :];Extract out part of rows: B = A[1:2, :];Extract out part of columns: C=A[:,1:2];Extract out part of columns: C=A[:,1:2];Operations on MatrixOperations on Matrix►►Cell by cell operation.Cell by cell operation.‘‘..’’E.g. B = A.^2; E.g. B = A.^2; B= [1 4 9; 16 25 36; 49; 64; 81];B= [1 4 9; 16 25 36; 49; 64; 81];►►Matrix operation.Matrix operation.‘‘++’’, , ‘‘--’’, , ‘‘**’’..Control Structures for Matlab(1)Control Structures for Matlab(1)►►Conditional statements.Conditional statements.►►ExampleExampleif expression1statements1elseif expression2statements2elsestatements3endif (a>3) b=4;end;Control Structure for Matlab(2)Control Structure for Matlab(2)►►Loop structure: for loopLoop structure: for loop►►Loop structure: while loop.Loop structure: while loop.for variable = expressionstatementsendwhile expressionstatements endj=0;for i=1:10j = j+i;endSymbolic Toolbox(1)Symbolic Toolbox(1)►►Declare a symbol object.Declare a symbol object.Not a number but a symbol.Not a number but a symbol.Syntax: Syntax: symssymsarg1 arg2 arg1 arg2 ……real.real.Use symbols to represent a function.Use symbols to represent a function.syms x u realsyms s positivef = exp(-(x-u)^2/s^2);Symbolic Toolbox(2)Symbolic Toolbox(2)►►Manipulate the function.Manipulate the function.Compute integration.Compute integration.g = g = int(f,xint(f,x, , --infinf, , infinf); result: g =s*pi^(1/2)); result: g =s*pi^(1/2)Gaussian distribution: Gaussian distribution: f/gf/g►►There are many other ways to manipulate There are many other ways to manipulate the functions: e.g. differentiation.the functions: e.g. differentiation.Load and save dataLoad and save data►►Load data:Load data:Matrix format: Matrix format: load(load(‘‘filefilepathpath’’););►►Save data:Save data:Matrix format: Matrix format: save(save(‘‘filefilepathpath’’, , ‘‘matrix namematrix name’’, , ‘‘--asciiascii’’););Common Functions in CSE 802Common Functions in CSE 802►►Functions related to Multivariate Gaussian distribution.Functions related to Multivariate Gaussian distribution.mean(Amean(A))cov(xcov(x), ), cov(x,ycov(x,y); x, y are vectors.); x, y are vectors.inv(Ainv(A): inverse of the matrix.): inverse of the matrix.det(Adet(A): determinant of the matrix.): determinant of the matrix.mvnrnd(mumvnrnd(mu, sigma, num of data.), sigma, num of data.)►►Functions related to dimensionality reduction.Functions related to dimensionality reduction.eigs(Aeigs(A): compute eigenvector of A.): compute eigenvector of A.PlottingPlotting►►Plot function:Plot function:Plot one line: plot(X1,Y1,LineSpec).Plot one line: plot(X1,Y1,LineSpec).Plot several lines on the same figure:Plot several lines on the same figure:►►figure(1);figure(1);►►hold on;hold on;►►plot(x1, y1, LineSpec1);plot(x1, y1, LineSpec1);►►plot(x2, y2, LineSpec2);plot(x2, y2, LineSpec2);►►……►►hold off;hold off;►►legend(legend(‘‘lineline11’’, , ‘‘line 2line 2’’, , ……););►►xlabel(xlabel(‘‘descriptiondescriptionof x axisof x axis’’); ); ylabel(ylabel(‘‘descriptiondescriptionof y axisof y axis’’););Plotting ExamplePlotting Examplex = 1:10 ;y = 3*x; z = x.^2; figure(1)hold on; plot(x, y, '-ro'); plot(x, z, '-b*'); hold off; legend('y=3*x', 'z=x.^2'); xlabel('x');ylabel('function values');EzplotEzplot►►Mainly used for implicitly defined functions.Mainly used for implicitly defined functions.Sometimes, itSometimes, it’’s more convenient to plot the implicit s more convenient to plot the implicit form of the functions.form of the functions.►►E.g.E.g.Function format: Function format: ezplot(f,[xmin,xmax,ymin,ymaxezplot(f,[xmin,xmax,ymin,ymax]) plots ]) plots f(x,yf(x,y) = 0 over ) = 0 over xminxmin< x < < x < xmaxxmaxand and yminymin< y < < y < ymaxymax. . ►►The first parameter f is passed as a string.The first parameter f is passed as a string.221xy+=EzplotEzplotExampleExamplefigure(1)ezplot('x^2+y^2-1',[-1,1,-1,1]);xlabel('x');ylabel('function values');Generate Multivariate Gaussian DataGenerate Multivariate Gaussian Data►►Generate multivariate Gaussian data.Generate multivariate Gaussian data.rand_datarand_data= = mvnrnd(mumvnrnd(mu, sigma, num of data.), sigma, num of data.)E.g.E.g.mu1 = [0 0];sigma1 = [1 0; 0 1];r1 = mvnrnd(mu1, sigma1, 50);plot(r1(:,1), r1(:,2), ‘*’);PCA to Extract the Major Information PCA to Extract the Major Information in
View Full Document