Everything in MATLAB is a matrix. Master creation, indexing, element-wise vs matrix operations, and tackle real linear algebra problems — eigenvalues, SVD, and least-squares — in the first session.
Everything in MATLAB is a matrix. Master creation, indexing, element-wise vs matrix operations, and tackle real linear algebra problems — eigenvalues, SVD, and least-squares — in the first session.
MATLAB (Matrix Laboratory) is a commercial numerical computing environment from MathWorks. It's the dominant tool in control systems, signal processing, aerospace, and biomedical engineering. The free alternative is GNU Octave, which is largely compatible with MATLAB syntax — all code in this course runs in both.
octave.org — it runs all the code in this course. If you have a MATLAB license, use MATLAB Online at matlab.mathworks.com for free browser access.% Row vector (semicolons separate rows; commas/spaces separate columns)
v = [1, 2, 3, 4, 5] % 1x5 row vector
% Column vector
c = [1; 2; 3; 4; 5] % 5x1 column vector
% 3x3 matrix
A = [1, 2, 3;
4, 5, 6;
7, 8, 9]
% Colon operator: start:step:end
x = 0:0.1:1 % 0, 0.1, 0.2 ... 1.0 (11 elements)
y = linspace(0, 2*pi, 100) % 100 evenly spaced from 0 to 2π
% Special matrices
I = eye(4) % 4x4 identity
Z = zeros(3, 5) % 3x5 all-zeros
O = ones(2, 4) % 2x4 all-ones
R = rand(3) % 3x3 uniform random [0,1]
N = randn(3) % 3x3 standard normal
% size, length, numel
disp(size(A)) % [3, 3]
disp(length(v)) % 5 (longest dimension)
disp(numel(A)) % 9 (total elements)
A = magic(4) % 4x4 magic square
% Single element: A(row, col) — 1-indexed
disp(A(2, 3)) % row 2, column 3
% Entire row or column
row2 = A(2, :) % all columns of row 2
col3 = A(:, 3) % all rows of column 3
% Submatrix: rows 1-2, cols 2-4
sub = A(1:2, 2:4)
% Last row/column using end
last_row = A(end, :)
corner = A(end-1:end, end-1:end) % bottom-right 2x2
% Linear indexing (column-major order)
A(5) % 5th element column-by-column
% Logical indexing
v = [3, -1, 4, -2, 5, -3];
pos = v(v > 0) % [3, 4, 5]
v(v < 0) = 0 % set negatives to zero: [3,0,4,0,5,0]
% reshape
M = reshape(1:12, 3, 4) % 3x4 matrix from 1 to 12
A = [1,2; 3,4];
B = [5,6; 7,8];
% Matrix multiplication (dot product rows × cols)
C = A * B % [19,22; 43,50]
% Element-wise multiplication (same size required)
D = A .* B % [5,12; 21,32]
% Element-wise division and power
E = A ./ B % [0.2,0.333...; 0.428...,0.5]
F = A .^ 2 % [1,4; 9,16]
% Transpose
At = A' % [1,3; 2,4] (conjugate transpose for complex)
At = A.' % same for real matrices
% Matrix inverse and pseudo-inverse
Ai = inv(A) % exact inverse (square, non-singular only)
Api = pinv(A) % Moore-Penrose pseudo-inverse
% Determinant and rank
det(A) % -2
rank(A) % 2
% Norms
norm(A) % 2-norm (largest singular value)
norm(A, 'fro') % Frobenius norm
The backslash operator \ is the fastest and most numerically stable way to solve Ax = b in MATLAB. Use it instead of inv(A) * b.
% Solve Ax = b (backslash — always prefer over inv(A)*b)
A = [2, 1, -1;
-3, -1, 2;
-2, 1, 2];
b = [8; -11; -3];
x = A \ b % [2; 3; -1]
% Verify
norm(A*x - b) % should be near zero (~1e-15)
% Least-squares solution (overdetermined: more equations than unknowns)
A2 = randn(10, 3);
b2 = randn(10, 1);
x2 = A2 \ b2 % minimum norm least-squares solution
% Eigenvalues and eigenvectors
[V, D] = eig(A) % D = diagonal eigenvalue matrix, V = eigenvectors
eigenvalues = diag(D)
% Singular Value Decomposition: A = U * S * V'
[U, S, V] = svd(A)
singular_values = diag(S)
% Low-rank approximation (keep top 2 singular values)
k = 2;
Ak = U(:,1:k) * S(1:k,1:k) * V(:,1:k)';
fprintf('Rank-%d approximation error: %.4f\n', k, norm(A - Ak, 'fro'))
; prints its result to the console. Add ; to suppress — this is a constant habit difference between interactive exploration (no semicolon) and script code (semicolons everywhere).img = double(imread('cameraman.tif')); (built-in MATLAB test image) or any PNG converted with rgb2gray.[U, S, V] = svd(img);compress(U, S, V, k) that reconstructs the image keeping only the top k singular values.: for ranges and slicing; end for the last index..*, ./, .^ for element-wise operations; *, / for matrix operations.A \ b to solve linear systems — never inv(A) * b (slower and less stable).Implement Principal Component Analysis (PCA) from scratch using SVD. Given a data matrix X (rows = observations, cols = features), center the data, compute the SVD, project onto the top k principal components, and reconstruct. Compare your result with MATLAB's built-in pca() function.
The foundations from today carry directly into Day 2. In the next session the focus shifts to Plotting and Data Visualization — building directly on everything covered here.
Before moving on, verify you can answer these without looking:
Live Bootcamp
Learn this in person — 2 days, 5 cities
Thu–Fri sessions in Denver, Los Angeles, New York, Chicago, and Dallas. $1,490 per seat. June–October 2026.
Reserve Your Seat →The foundations from today carry directly into Day 2. In the next session the focus shifts to Plotting and Data Visualization — building directly on everything covered here.
Before moving on, verify you can answer these without looking:
Live Bootcamp
Learn this in person — 2 days, 5 cities
Thu–Fri sessions in Denver, Los Angeles, New York, Chicago, and Dallas. $1,490 per seat. June–October 2026.
Reserve Your Seat →