Download MATLAB Cheat Sheet and more Exams Matlab skills in PDF only on Docsity! MATLAB Cheat Sheet Basic Commands % Indicates rest of line is commented out. ; If used at end of command it suppresses output. If used within matrix definitions it indicates the end of a row. save filename Saves all variables currently in workspace to file filename.mat. save filename x y z Saves x, y, and z to file filename.mat. save -append filename x Appends file filename.mat by adding x. load filename Loads variables from file filename.mat to workspace. ! Indicates that following command is meant for the operating system. ... Indicates that command continues on next line. help function/command Displays information about the function/command. clear Deletes all variables from current workspace. clear all Basically same as clear. clear x y Deletes x and y from current workspace. home Moves cursor to top of command window. clc Homes cursor and clears command window. close Closes current figure window. close all Closes all open figure windows. close(H) Closes figure with handle H. global x y Defines x and y as having global scope. keyboard When placed in an M-file, stops execution of the file and gives control to the user’s keyboard. Type return to return control to the M-file or dbquit to terminate program. Plotting Commands figure(H) Makes H the current figure. If H does not exist is creates H. Note that H must be a positive integer. plot(x,y) Cartesian plot of x versus y. plot(y) Plots columns of y versus their index. plot(x,y,‘s’) Plots x versus y according to rules outlined by s. semilogx(x,y) Plots log(x) versus y. semilogy(x,y) Plots x versus log(y). loglog(x,y) Plots log(x) versus log(y). grid Adds grid to current figure. title(‘text’) Adds title text to current figure. xlabel(‘text’) Adds x-axis label text to current figure. ylabel(‘text’) Adds y-axis label text to current figure. hold on Holds current figure as is so subsequent plotting commands add to existing graph. hold off Restores hold to default where plots are overwritten by new plots. 1 Creating Matrices/Special Matrices A=[1 2;3 4] Defines A as a 2-by-2 matrix where the first row contains the numbers 1, 2 and the second row contains the number 3, 4. B=[1:1:10] Defines B as a vector of length 10 that contains the numbers 1 through 10. A=zeros(n) Defines A as an n-by-n matrix of zeros. A=zeros(m,n) Defines A as an m-by-n matrix of zeros. A=ones(n) Defines A as an n-by-n matrix of ones. A=ones(n,m) Defines A as an m-by-n matrix of ones. A=eye(n) Defines A as an n-by-n identity matrix. A=repmat(x,m,n) Defines A as an m-by-n matrix in which each element is x. Matrix Operations A*B Matrix multiplication. Number of columns of A must equal number of rows of B. Aˆn A must be a square matrix. If n is an integer and n > 1 than Aˆn is A multiplied with itself n times. Otherwise, Aˆn is the solution to Anvi = livi where li is an eigenvalue of A and vi is the corresponding eigenvector. A/B This is equivalent to A*inv(B) but computed more efficiently. A\B This is equivalent to inv(A)*B but computed more efficiently. A.*B,A./B, Element-by-element operations. A.\B,A.\ˆn A’ Returns the transpose of A. inv(A) Returns the inverse of A. length(A) Returns the larger of the number of rows and columns of A. size(A) Returns of vector that contains the dimensions of A. reshape(A,m,n) Reshapes A into an m-by-n matrix. kron(A,B)) Computes the Kronecker tensor product of A with B. A = [A X] Concatenates the m-by-n matrix A by adding the m-by-k matrix X as additional columns. A = [A; Y] Concatenates the m-by-n matrix A by adding the k-by-n vector Y as additional rows. 2