MATLAB Cheat Sheet

+ - * / \ ^ (matrix), .* ./ .^ (element-wise), ' .' (transposes), == ~= < <= > >=, & | ~ xor, && ||.

Concept

% text, block %{... %} Continue a line help f, doc f, lookfor word

who, whos, clear x, clc, close all disp(x), fprintf('%d %.2f %s %g\n',...), sprintf

[1 2 3], [1; 2], a:s:b, linspace, zeros, ones, eye, rand, magic

size, numel, length, ndims, isempty A(r, c), A(:, k), A(end,:), v(mask), A(k) (linear)

A', reshape, A(:), repmat, cat, [A B], [A; B] v(k) = [], v(end + 1) = x

find, any, all, sort, unique, ismember, max, min

+ - * / \ ^ (matrix), .* ./ .^ (element-wise), ' .' (transposes), == ~= < <= > >=, & | ~ xor, && ||.

3, int16(3), single(3) true, v > 0 s(1:3), strcmp, sprintf String (MATLAB) +, contains, split c{k}, c(k) s.f = 1, struct(...) s.f, s.(name), [s.f] m(key), isKey, keys Table (MATLAB) table(...), readtable T.Var, T(rows,:)

readmatrix/writematrix, readtable/writetable, csvread/csvwrite, fopen/fprintf/fgetl/fclose, textscan, save/load, jsondecode/jsonencode, fullfile, dir, exist.

plot, scatter, bar, histogram/hist, stem, semilogx, loglog, surf, mesh, contour, plot3, imagesc, subplot/tiledlayout, hold on, xlabel, ylabel, title, legend, grid on, axis, colorbar, print/exportgraphics.

A \ b, lu, qr, chol, eig, svd, cond, rank roots, polyval, polyfit, conv, polyder interp1, interp2, spline, pchip trapz, cumtrapz, integral ode45, ode15s, odeset

fzero, fminbnd, fminsearch (+ toolbox fmincon, lsqcurvefit)

fft, ifft, filter, conv, movmean mean, median, std, var, corrcoef, accumarray, histc

tic/toc, timeit, profile on/viewer, preallocate with zeros, vectorize, parfor, gpuArray.

if c, ..., elseif c2, ..., else, ..., end
switch x, case 1, ..., case {2, 3}, ..., otherwise, ..., end
for k = 1:n, ..., end
while c, ..., end            % break, continue
try, ..., catch err, ..., end
function [a, b] = f(x, y)    % file f.m
g = @(x) x.^2;               % anonymous
h = @sin;                    % handle
arrayfun(@f, A); cellfun(@f, C, 'UniformOutput', false);

Why It Matters

A compact reference speeds up daily work and interview preparation.

Example

The cheat sheet summarizes code that is demonstrated and verified in Modules 1–31.

Expected Result

Not applicable — use the tables as a quick reference.

Common Mistakes

Forgetting which functions need a toolbox (marked in Module 31).

Mixing MATLAB-only syntax ("...", table) into code that must run in Octave or older releases.

Best Practices

Keep your own cheat sheet of snippets from your projects.

Use doc for exact options — MATLAB functions have many name–value arguments.

Interview Relevance

Revision material for all interview levels.

Practice Questions

Without looking, write the code to read a CSV, compute column means and plot them as a bar chart.

List five functions that return two outputs and what the second output means.

Write the three ways to create the vector 0, 0.25, …, 1.

Which cheat-sheet functions are MATLAB-only?

Quick Quiz

Q1. (Multiple choice) Which continues a statement on the next line? A) \ B) ... C) ; D) % Answer: B.

Q2. (True/False) A(:) returns all elements of A as a column vector.

Answer: True. Q3. (Predict the output) numel(0:0.25:1) Answer: 5.

Q4. (Find the bug) x = linspace(0, 1, 0.1); Answer: The third argument is the number of points, not the step; use 0:0.1:1 or linspace(0, 1, 11).

Coding Challenge

Create the vector 0, 0.25, …, 1 in three different ways and verify that all three are equal.

a = 0:0.25:1;
b = linspace(0, 1, 5);
c = (0:4) / 4;
fprintf('equal: %d %d\n', isequal(a, b), isequal(b, c))