Skip to content
MATLAB Compatibility and Idioms
step 1/4

Reading — step 1 of 4

Learn

~2 min readVectorization, ODEs, Optimization

Octave is >95% MATLAB-compatible. Most MATLAB code runs unmodified. Knowing the differences helps you write portable code.

What's identical

  • Core syntax (matrices, cell arrays, structs, control flow)
  • Most numerical functions (linspace, zeros, eye, etc.)
  • Linear algebra (A\b, eig, svd)
  • Plotting (mostly — small visual differences)
  • File I/O (load, save, csvread, etc.)

Key differences

1. Strings — Octave allows double quotes, MATLAB requires single in older versions:

s1 = 'hello';     %% works in both
s2 = "hello";     %% Octave + MATLAB R2017a+

For cross-version: stick with single quotes.

2. End keywords — Octave allows specific:

if x > 0
    %% body
endif      %% Octave-specific (also `end`)

MATLAB only accepts end. For portability, use end.

3. Comments:

% MATLAB and Octave
# Octave-only

Use %.

4. Some functions differ slightly — e.g., regexp flags, printf (Octave) vs fprintf (MATLAB).

Idiomatic patterns

End-conditioned loops:

for i = length(v):-1:1     %% iterate backwards
    process(v(i));
end

Pre-allocate before loops (huge speedup for non-vectorizable code):

result = zeros(1, N);          %% pre-allocate
for i = 1:N
    result(i) = compute(i);
end

Without pre-allocation, each result(end+1) = ... reallocates the entire array.

Avoid for over a vector — vectorize:

%% slow
result = zeros(size(v));
for i = 1:length(v)
    result(i) = v(i)^2 + 1;
end

%% fast
result = v.^2 + 1;

Sort then act:

[sorted, idx] = sort(v);
original_positions = idx;       %% useful for ranking, percentiles

Toolboxes / packages

Octave Forge packages are roughly equivalent to MATLAB toolboxes. Install once:

pkg install -forge statistics
pkg load statistics

Available: signal, image, optim, control, symbolic, io, parallel, ga (genetic algorithms), nan (NaN-tolerant ops).

MATLAB toolboxes that DON'T have Octave equivalents:

  • Simulink (graphical block diagrams)
  • Stateflow
  • Several specialized commercial toolboxes

But for typical numerical/scientific work, the open Octave packages cover it.

Learning path

If you know MATLAB → you know Octave (mostly). If you know NumPy → it's familiar but the matrix-first orientation differs from NumPy's array-first. If you're new → Octave/MATLAB rewards thinking in linear algebra. Resist explicit loops, embrace vectorization.

For large-scale modern numerical work, Julia is a strong alternative (more expressive, faster). For ML, Python + NumPy/PyTorch is the standard. Octave/MATLAB still own academic engineering, signal processing, and control systems.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…