MATLAB / Octave Tutorial
Learn MATLAB / Octave step by step: 45 lessons in 12 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Are MATLAB and Octave? Learn what MATLAB and GNU Octave are, how they differ, how compatible they are, what people use them for, and how this tutorial shows the code.
- The Desktop, Command Window and Scripts Tour the MATLAB and Octave desktop: command window, editor, workspace, current folder, scripts and live scripts, the ans variable and the semicolon.
- Variables, Output and Help Create variables, follow the naming rules, print with disp and fprintf, control number display with format, and find answers with help, doc and lookfor.
Matrices and Arrays
- Everything Is a Matrix Understand why everything in MATLAB and Octave is a matrix: scalars, row and column vectors, creating matrices, size, numel, transpose and 1-based indexing.
- Ranges and Special Matrices Create vectors and matrices fast in MATLAB and Octave: the colon operator, linspace, logspace, zeros, ones, eye, diag, rand, randi, magic, repmat and meshgrid.
- Indexing and Slicing Pick elements, rows, columns and blocks from MATLAB and Octave arrays with subscripts, the colon, end, linear indexing, sub2ind, deletion and growth.
- Logical Indexing and find Select, count and change MATLAB or Octave array elements with comparison masks, find, any, all and nnz, with runnable Python stand-ins.
- Reshaping, Joining and Sorting Reshape arrays, join them with horzcat, vertcat and cat, and sort with sort, sortrows and unique in MATLAB and Octave, with Python stand-ins.
Operators and Math
- Arithmetic: Matrix vs Element-wise Learn when MATLAB and Octave operators work on whole matrices or element by element: * vs .*, ^ vs .^, transpose, broadcasting and precedence.
- Linear Algebra Essentials Use MATLAB and Octave for determinants, inverses, rank, norms, solving Ax=b with backslash, eigenvalues, SVD, LU, QR and Cholesky, with Python stand-ins.
- Built-in Math and Statistics Summarize data in MATLAB and Octave with sum, cumsum, mean, median, std, max with indices, round, mod and NaN handling, using runnable Python stand-ins.
- Numeric Types and Precision Understand double, single and integer types, saturation arithmetic, eps, floating-point surprises, Inf, NaN and complex numbers in MATLAB and Octave.
Data Types and Text
- Strings and Character Arrays Learn text in MATLAB and Octave: char arrays versus string objects, joining, num2str, str2double, sprintf, strsplit, strrep and regexp, with Python stand-ins.
- Cell Arrays Learn MATLAB and Octave cell arrays: braces versus parentheses, creating and indexing cells, cellfun, cell2mat, mixed data and comma-separated lists.
- Structures Learn MATLAB and Octave structures: named fields, dynamic field names, struct arrays, arrayfun, nested structs, isfield and rmfield, with Python stand-ins.
- Tables and Maps Learn MATLAB table variables and containers.Map key-value lookups, grouping, sorting and what Octave does differently, with pandas and dict stand-ins.
Programming
- if, switch and Loops Learn MATLAB and Octave control flow: if/elseif, switch with strings and cells, for loops over columns, while, break, continue and why loops can be slow.
- Scripts and Functions Learn how MATLAB and Octave scripts differ from function files, return multiple outputs, use nargin and nargout, set defaults and keep variables in scope.
- Anonymous Functions and Function Handles Learn the @ syntax for anonymous functions and function handles in MATLAB and Octave: closures, cellfun, arrayfun, feval, and passing handles to fzero and integral.
- Errors, Warnings and Debugging Raise and handle errors in MATLAB and Octave with error, warning, try/catch, MException and assert, use breakpoints and dbstop, and decode common error messages.
Vectorization and Performance
- Vectorization Learn vectorization in MATLAB and Octave: replace loops with array operations, use implicit expansion, cumsum and diff tricks, and see why it is faster.
- Performance and Memory Speed up MATLAB and Octave code: preallocate arrays, time with tic and toc, use the profiler, respect column-major order, avoid growth, and know when to go parallel.
Plotting and Visualization
- 2-D Plots Draw 2-D line plots in MATLAB and Octave: plot, line styles, hold on, labels, legend, axis limits, subplot, tiledlayout and saving figures with exportgraphics or print.
- Bar, Scatter, Histogram and Images Choose the right chart in MATLAB and Octave: bar, scatter, histogram, why to avoid pie, imagesc and colormap, semilogx and loglog axes, and errorbar, with matplotlib stand-ins.
- 3-D Plots, Surfaces and Contours Draw 3-D curves, surfaces and contour maps in MATLAB and Octave with plot3, meshgrid, surf, mesh, contour, view and colorbar, plus runnable matplotlib stand-ins.
Data and Files
- Reading and Writing Files Read and write text, CSV and MAT files in MATLAB and Octave with fopen, fgetl, fprintf, fclose, load, save, readmatrix and importdata, with in-memory Python stand-ins.
- Cleaning and Preparing Data Clean data in MATLAB and Octave: find missing values, detect outliers, fill gaps with interpolation, smooth with movmean, normalize, sort and group, with Python stand-ins.
Numerical Methods
- Polynomials and Curve Fitting Work with polynomials in MATLAB and Octave: polyfit, polyval, roots, conv, least squares with the backslash operator, residuals and interp1, with Python stand-ins.
- Solving Equations and Optimization Solve nonlinear equations and minimize functions in MATLAB and Octave with fzero, fsolve, fminsearch, fminunc, fmincon and linprog, with scipy.optimize stand-ins.
- Integration, Differentiation and ODEs Do calculus numerically in MATLAB and Octave: trapz, integral, diff, gradient, ode45 and stiff solvers like ode15s, events, with SciPy solve_ivp stand-ins.
- Statistics and Random Numbers Generate random numbers with rand, randn and rng, summarize data, measure correlation and covariance, and run a Monte Carlo estimate, with Python stand-ins.
- Signals and the FFT Sample a signal, find its frequencies with fft, build a frequency axis, smooth with filter and conv, and plot a spectrum in MATLAB and Octave, with Python stand-ins.
- Symbolic Math Do algebra and calculus with exact formulas in MATLAB and Octave: syms, solve, diff, int, simplify, limits and dsolve, with SymPy stand-ins you can run.
Modeling and Applications
- Image Processing Basics Treat images as matrices in MATLAB and Octave: imread and imshow, grayscale, conv2 filters, thresholding and edge detection, with runnable NumPy stand-ins.
- Simulink, Toolboxes and Octave Packages What Simulink is, how MATLAB toolboxes and Octave packages extend the core, how pkg load works, and how to check licenses honestly, with simulation stand-ins.
Real-World Projects
- Project: Fit a Model to Data Build a linear regression in MATLAB and Octave: simulate data, solve the normal equations and backslash, compute R-squared, plot residuals and predict, with Python stand-ins.
- Project: Predator-Prey Simulation Simulate rabbits and foxes with the Lotka-Volterra equations in MATLAB and Octave using ode45, draw a phase plot and run a parameter study, with SciPy stand-ins.
- Project: Denoise a Signal Clean a noisy sine wave in MATLAB and Octave with a moving average and an FFT low-pass filter, compare their errors and learn how to choose parameters.
- Project: Enhance an Image Improve a dull synthetic image in MATLAB and Octave with histogram equalization, conv2 blur and sharpen kernels and a Sobel edge map, with NumPy stand-ins.
- Project: Monte Carlo Estimates Estimate pi, simulate random walks and price an option-style payoff with vectorized Monte Carlo in MATLAB and Octave, and see error shrink with sample size.
Practice and Review
- MATLAB, Octave and Python Compared Compare MATLAB, GNU Octave and Python with NumPy side by side: syntax differences, portable code, Octave-only and MATLAB-only features, and translation tips.
- Common Mistakes and How to Fix Them Avoid the classic MATLAB and Octave traps: 1-based indexing, column-major order, element-wise operators, size mismatches, shadowed built-ins and float comparisons.
- Practice Exercises with Solutions Twenty graded MATLAB and Octave exercises on matrices, loops, functions, plots and numerical methods, with collapsible solutions and Python stand-ins.
- Interview Questions Prepare for MATLAB and Octave interviews with clear answers on arrays, vectorization, cells vs structs, performance and floating-point numerics.
- MATLAB / Octave Cheat Sheet A one-page MATLAB and Octave cheat sheet covering syntax, operators, indexing, common functions, plotting and the main Octave differences.