MATLAB Cheat Sheet
Key MATLAB syntax for matrices, control flow, function definitions, and plotting used in numerical computing.
2 PagesBeginnerApr 15, 2026
Basics
Variables and output.
matlab
% Variables and basic I/Ox = 42;name = 'MATLAB';disp(x)fprintf('Value: %d\n', x)y = 3.14 % without semicolon, prints result
Matrices
Creating and operating on matrices.
matlab
A = [1 2 3; 4 5 6; 7 8 9]; % 3x3 matrixB = zeros(3,3);C = A'; % transposeD = A * B; % matrix multiplicationE = A .* A; % element-wise multiplicationv = A(2,:); % second roww = A(:,1); % first columninvA = inv(A); % matrix inverse (if invertible)
Control Flow
Loops and conditionals.
matlab
for i = 1:5 fprintf('i = %d\n', i);endx = 10;if x > 5 disp('big')elseif x == 5 disp('equal')else disp('small')endwhile x > 0 x = x - 1;end
Functions
Defining and calling functions.
- function y = f(x)- Defines a function; typically one per .m file
- nargin / nargout- Number of input/output arguments actually supplied
- anonymous function- f = @(x) x^2; creates an inline function handle
- size(A) / length(v)- Dimensions of a matrix / length of a vector
- disp / fprintf- Print a value / print formatted text
Plotting
Basic 2D plotting.
matlab
x = linspace(0, 2*pi, 100);y = sin(x);plot(x, y)xlabel('x')ylabel('sin(x)')title('Sine Wave')grid on
Linear Algebra
Solving systems and matrix decompositions.
matlab
A = [2 1; 1 3];b = [3; 5];x = A \ b; % solve Ax = b (preferred over inv)[L, U, P] = lu(A); % LU decomposition[V, D] = eig(A); % eigenvectors V, eigenvalues Dr = rank(A);d = det(A);At = A'; % transpose
Matrix Constructors
Functions that generate common matrices.
- zeros(m,n)- m-by-n matrix of zeros
- ones(m,n)- m-by-n matrix of ones
- eye(n)- n-by-n identity matrix
- linspace(a,b,n)- n evenly spaced points from a to b
- reshape(A,m,n)- reshape A to m-by-n keeping element order
- repmat(A,m,n)- tile copies of A into an m-by-n block grid
- rand(n)- n-by-n matrix of uniform random values in [0,1]
Cell Arrays & Structs
Heterogeneous containers and named fields.
matlab
c = {1, 'text', [1 2 3]}; % cell arrayc{2} % access -> 'text's.name = 'Ada';s.age = 36;fields = fieldnames(s);% struct arraypeople(1).id = 1;people(2).id = 2;ids = [people.id];
Vectorization & Logical Indexing
Replace loops with array operations.
matlab
v = 1:10;squares = v.^2; % element-wise powermask = v > 5; % logical arraybig = v(mask); % keep elements > 5v(v < 0) = 0; % clamp negatives to zerototals = sum(reshape(v, 2, 5), 1); % column sums
String Functions
Working with char arrays and string objects.
- sprintf- format values into a string using C-style specifiers
- strcat- concatenate strings horizontally
- strsplit- split a string on a delimiter into a cell array
- strrep- replace all occurrences of a substring
- regexp- match or tokenize using regular expressions
- num2str- convert a numeric value to its string form
Pro Tip
Preallocate arrays with zeros()/ones() before loops — growing arrays dynamically inside a loop is one of the most common MATLAB performance killers.
Was this cheat sheet helpful?
Explore Topics
#MATLAB#MATLABCheatSheet#Programming#Beginner#Matrices#ControlFlow#Functions#Plotting#CheatSheet#SkillVeris