Transcription of MATLAB Cheat Sheet - Michigan State University
1 MATLAB Cheat SheetBasic Commands%Indicates rest of line is commented out.;If used at end of command it suppresses used within matrix definitions it indicates the end of a filenameSaves all variables currently in workspace to filename x y zSavesx,y, andzto -append filename xAppends filenameLoads variables from workspace.!Indicates that following command is meant for the operating that command continues on next function/commandDisplays information about the all variables from current allBasically same as x yDeletesxandyfrom current cursor to top of command cursor and clears command current figure allCloses all open figure (H)Closes figure with x yDefinesxandyas having global placed in an M-file, stops execution of the file and givescontrol to the user s keyboard.
2 Typereturnto return controlto the M-file ordbquitto terminate ( data ,..Sets A to be a 5-by-2 matrix of the data contained in sheet1 , a3:b7 )cells A3 through B7 of sheetsheet1of excel (..Writes contents of A to sheetsheet1of excel file results ,A, sheet1 , c7 ) at cell C7. If successfulsuccess= the current search path c:\my_functionsAdds directoryc:\my_functionsto top of current search c:\my_functionsRemoves directoryc:\my_functionsfrom current search ( random statement )Printsrandom statementin the command (x)Prints only the value ofxon command ([ x= ,num2str(x,5)])Displaysx=and first 5 digits ofxon command window. Only workswhenxis scalar or row (..DisplaysThe 3 is command window. The %g is % \n , x,sqrt(x))format shortDisplays numeric values in floating point format with 4 digits afterthe decimal longDisplays numeric values in floating point format with 15 digits afterthe decimal Commandsfigure(H)MakesHthe current figure.
3 IfHdoes not exist is thatHmust be a positive (x,y)Cartesian plot (y)Plots columns ofyversus their (x,y, s )Plotsxversusyaccording to rules outlined (x,y)Plotslog(x) (x,y)Plotsxversuslog(y).loglog(x,y)Plots log(x)versuslog(y).gridAdds grid to current ( text )Adds titletextto current ( text )Adds x-axis labeltextto current ( text )Adds y-axis labeltextto current onHolds current figure as is so subsequent plotting commands addto existing offRestores hold to default where plots are overwritten by new Matrices/Special MatricesA=[1 2;3 4]DefinesAas a 2-by-2 matrix where the first row contains thenumbers 1, 2 and the second row contains the number 3, [1:1:10]DefinesBas a vector of length 10 that contains the numbers1 through (n)DefinesAas an n-by-n matrix of (m,n)DefinesAas an m-by-n matrix of (n)DefinesAas an n-by-n matrix of (n,m)DefinesAas an m-by-n matrix of (n)DefinesAas an n-by-n identity (x,m,n)DefinesAas an m-by-n matrix in which each element (x1, x2, n)Generatesnpoints OperationsA*BMatrix multiplication.
4 Number of columns of A must equal numberof rows of nAmust be a square matrix. Ifnis an integer andn >1thanA nisAmultiplied with itselfntimes. Otherwise,A nis the solution toAnvi=liviwhereliis an eigenvalue ofAandviis the is equivalent toA*inv(B)but computed more \BThis is equivalent toinv(A)*Bbut computed more *B, ,Element-by-element \B,A. nA Returns the transpose (A)Returns the inverse (A)Returns the larger of the number of rows and columns (A)Returns of vector that contains the dimensions (A,1)Returns the number of rows (A,m,n)ReshapesAinto an m-by-n (A,B)Computes the Kronecker tensor product = [A X]Concatenates the m-by-n matrixAby adding the m-by-k matrix X asadditional = [A; Y]Concatenates the m-by-n matrixAby adding the k-by-n vector Y asadditional Analysis Commandsrand(m,n)Generates an m-by-n matrix of uniformly distributed random (m,n)Generates an m-by-n matrix of normally distributed random (x)Ifxis a vector it returns the largest element a matrix it returns a row vector of the largest element in eachcolumn (x)Same asmaxbut returns the smallest element (x)Ifxis a vector it returns the mean of the elements a matrix it returns a row vector of the means for each column (x)Ifxis a vector it returns the sum of the elements a matrix it returns a row vector of the sums for each column (x)Same assumbut returns the product of the elements (x)
5 Ifxis a vector it returns the standard deviation of the elements a matrix it returns a row vector of the standard deviations for eachcolumn (x)Same asstdbut returns the variance of the elements and Loopsfor i=1:10procedureIterates overprocedureincrementingifrom 1 to 10 by (criteria)procedureIterates overprocedureas long ascriteriais (criteria 1)Ifcriteria 1is true doprocedure1, else ifcriteria 2is true doprocedure 2, else doprocedure 1elseif(criteria 2)procedure 2elseprocedure 3end3