๐Ÿ”ข

MATLAB Cheat Sheet

The standard tool in most engineering departments โ€” signal processing, control systems, and numerical computing. Everything in MATLAB is fundamentally a matrix, even a single number.

๐Ÿ“ Reference page only, and this one's a bit different from the rest of this site: MATLAB is commercial, licensed software made by MathWorks โ€” unlike Python, C, or even R, there's no free, open runtime we could ever wire up here. Most students get access through a university license, or MathWorks' free tier for small personal use. GNU Octave is a free, open-source alternative with very similar syntax if you want to actually run these examples without a license.
Jump to: Matrices & vectorsIndexingVectorized operations Control flowFunctionsPlotting

Matrices & vectors

v = [1 2 3 4];              % a row vector
m = [1 2; 3 4];              % a 2x2 matrix โ€” semicolons separate rows
size(m)                    % [2 2]
zeros(3,3)                 % a 3x3 matrix of all zeros

Indexing

v(1)          % 1 โ€” MATLAB is 1-indexed, not 0-indexed!
m(2,1)        % row 2, column 1 -> 3
v(2:3)        % elements 2 through 3 -> [2 3]
m(:,1)        % the entire first column

Vectorized operations

Like R, MATLAB applies math to a whole vector/matrix at once โ€” no manual loop needed.

v * 2           % every element doubled
v .* v         % element-wise multiply (note the DOT โ€” plain * means matrix multiply)
sum(v)          % 10
mean(v)         % 2.5

Control flow

x = 10;
if x > 5
    disp('Big')
elseif x > 0
    disp('Small positive')
else
    disp('Not positive')
end

for i = 1:5
    disp(i)
end

Functions

function result = add(a, b)
    result = a + b;
end

% add(2, 3) -> 5

Plotting

x = 0:0.1:10;
y = sin(x);
plot(x, y)
xlabel('x'); ylabel('sin(x)'); title('Sine wave')
โ† See the R cheat sheet