MATLAB Cheat Sheet
MATLAB Cheat Sheet
SCIENCE
PROGRAMMING
ACADEMY WITH MATLAB
MATLAB (MATrix LABoratory) is a high-performance language for technical computing.
It integrates computation, visualization, and programming in an easy-to-use environment
where problems and solutions are expressed in familiar mathematical notation.
Small variables like x and y will be either row or column vectors and A will always be a matrix.
zeros(5, 5) Create a 5 × 5 matrix of zeros (Pre-Allocation) x+y Elementwise addition of two vectors x and y
SCIENCE
PROGRAMMING
ACADEMY WITH MATLAB
MATLAB (MATrix LABoratory) is a high-performance language for technical computing.
It integrates computation, visualization, and programming in an easy-to-use environment
where problems and solutions are expressed in familiar mathematical notation.
Small variables like x and y will be either row or column vectors and A will always be a matrix.
PLOTTING CONSTANTS
plot(x,y) Plot y versus x (same dimension!) pi π = 3.1415926533
loglog(x,y) Plot y versus x on a log-log Inf Infinity
scale (both axes have a log NaN Not a number (i.e. 0/0)
scale)
realmax Largest positive floating-point
semilogx(x, y) Plot y versus x with x on a number 1.7977 · 10308
log scale
realmin Smallest positive floating-point
semilogy(x, y) Plot y versus x with y on a number 2.2251·10−308
log scale
axis equal Force the x and y axes to be
scaled equally DEBUGGING
title('A Title') Add a title to the plot
tic Starts timer
xlabel('x text') Add a label to the x axis
toc Stops timer
ylabel('y text') Add a label to the y axis
try/catch Good to track errors
legend('foo', 'bar') Label 2 curves for the plot
dbclear Clears breakpoints
grid on/off Add a grid to the plot
break Terminates execution of for/while loop
figure Start a new plot
figure(i) ith figure
subplot(a,b,c) For multiple figures in one DATA IMPORT & EXPORT
plot
hold on Retains current figure when xlsread/xlswrite Spreadsheets (.xls,.xlsm)
adding new stuf load/save -ascii Text files (txt,csv)
hold off Default settings (no hold load/save Matlab Files (.m)
on!)
imread/imwrite Image Files
set(fig1, ’LineStyle’, ’-’) Change dot marker
set(fig1, ’Marker’, ’.’) Change marker type SOLVING LINEAR EQUATIONS
set(fig1, ’MarkerSize’, 10) Change marker size
set(fig1, ’FontSize’, 14) Change font size inv(A) Compute the inverse A−1
eig(A) Compute the eigenvalues of A
[L,U,P] = lu(A) The LU factorization P A = LU
ENTRIES OF MATRICES & VECTORS
[V,D] = eig(A) V are the eigenvectors of A, and the
x(5:8) The 5th to the 8th elements of x diagonals diag(D) are the
eigenvalues of A
x(5:end) The 5th to the last elements of x
A\b Compute the solution x to Ax = b
x(1:2:end) Every second element of x from the
first to last
A(3,:) Get the third row of A
A(:,5) Get the 5th column of A
A(5, 2:5) Get the first to fifth elements
in the 5th row
SCIENCE
PROGRAMMING
ACADEMY WITH MATLAB
CONDITIONAL STATEMENT
if a > 90
disp('Greater than 90');
elseif a == 90
disp('a is 90'); x = linspace(0,10,50);
else y = rand(50,1);
disp('None of the conditions is mets');
subplot(2,2,1), plot(x,sin(x),'Color','red','LineWidth',3)
end set(gca,'fontsize',14)
axis([0,2*pi,-1,1]), axis square