Design Philosophy and Coming from Other Languages
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. The reference material consulted while preparing these pages is listed in the section bibliography. |
Written against MATLAB R2026b. The short sessions below were run in GNU Octave 11.3.0 (MATLAB code that Octave also accepts) and their captured output is shown; MATLAB prints the same values with a different layout. Statements about MATLAB-only behaviour are checked against the R2026b documentation and labelled when they were not run.
This page is for readers who already program. It explains why MATLAB looks the way it does, then maps the idioms of Python with NumPy, R, Julia, Fortran and C onto it. If you have never programmed, start with Getting Started and come back later.
Everything is an array
MATLAB began as an interactive front end to the Fortran matrix libraries LINPACK and EISPACK, and its first version had a single data type, the matrix (Moler and Little, "A History of MATLAB"). That origin still shapes the language:
-
A scalar is a 1-by-1 array, a row vector is 1-by-n, a column vector is n-by-1, and a character string or a boolean is an array of characters or logicals.
-
There is no separate "list" or "vector" class, and a function written for a scalar almost always works for an array.
-
Binary operators work on whole arrays:
A*Bis the matrix product andA.*Bmultiplies element by element.
Newer data types (cell arrays, structures, tables, dictionary, classes) were added later and are still arrays at heart: a structure array has a size, and so does an object array.
The design decisions that matter
One-based indexing
The first element of v is v(1) and the last is v(end). Subscripts are written with parentheses, not square brackets, because indexing and calling a function look the same: f(3) is either one, and which one depends on whether f is a variable.
Column-major storage
Elements of a matrix are stored column by column, as in Fortran. Linear indexing with one subscript therefore walks down the first column, then the second, and so on. This is the single most important fact when you connect MATLAB with C (row-major) or NumPy (row-major by default).
v = [10 20 30 40];
v(1)
v(end)
M = [1 2 3; 4 5 6];
M(:)'
M(2, :)
M(5)
Octave 11.3.0 output:
ans = 10
ans = 40
ans =
1 4 2 5 3 6
ans =
4 5 6
ans = 3
M(:)' flattens the matrix into a column and transposes it back to a row, and the flattened order is 1 4 2 5 3 6, column by column. M(5) is therefore 3: the fifth element in storage order is the first element of the third column.
Value semantics and copy-on-write
Assigning an array to another variable behaves as if it made an independent copy. Changing one never changes the other, and there are no references to numeric arrays that you can alias by accident. MATLAB makes this cheap by sharing the data until one of the two variables is modified (copy-on-write), so the copy happens only when it is needed. Handle classes, covered on Value and Handle Classes, are the deliberate exception.
a = [1 2 3];
b = a;
b(1) = 99;
a
Octave 11.3.0 output:
a =
1 2 3
a is unchanged. The consequence for performance is that passing a large array to a function does not copy it unless the function modifies it.
Dynamic typing
Variables have no declared type, and the same name can hold a number now and a string later. The default numeric type is double; integer, single-precision and logical types are opt-in. Function arguments are not typed either, although since R2019b an arguments block can validate and convert them (see Argument Validation).
Vectorization first
Idiomatic MATLAB expresses a computation on a whole array in one statement and lets the built-in library loop in compiled code. The heatflow Laplacian can be written with loops, with indexing or with implicit expansion; the second and third are shorter than the loop. The indexed form is also much faster than the loop, whereas the dense matrix form is slower than indexing in the Octave timings that Vectorization and Preallocation reports, so shorter is not always faster. Since the introduction of the JIT-accelerated execution engine, plain loops are no longer the disaster they were in the 1990s, but they remain the slower choice for array arithmetic.
The interpreter and its JIT execution engine
MATLAB code is compiled to an internal form when it is run and executed by an engine that includes just-in-time acceleration. You do not compile a MATLAB program ahead of time to run it, there is no separate build step for ordinary code, and a change to a function file takes effect the next time the function is called. The Code Analyzer (see Code Analyzer and Code Quality) takes the place of a compiler’s warnings.
Mapping from other languages
The tables compare idioms, not features. Entries marked "not run here" were checked against the documentation of each language only.
From Python and NumPy
Python programmers will find MATLAB the closest of these languages to NumPy, which was modelled on it. See also Python Numbers for the Python side of the arithmetic.
| Task | Python and NumPy | MATLAB |
|---|---|---|
Index base |
0 |
1 |
Last element |
|
|
Slice of the first three |
|
|
Element access |
|
|
Matrix product |
|
|
Element-wise product |
|
|
Power |
|
|
Transpose |
|
|
Solve a linear system |
|
|
Broadcasting |
rules of NumPy broadcasting |
implicit expansion (R2016b) |
Boolean operators on arrays |
|
|
Not equal |
|
|
Increment |
|
|
Comment |
|
|
Block end |
indentation |
|
Strings |
|
|
Four differences cause most bugs:
-
Row-major against column-major. NumPy arrays are row-major by default, MATLAB arrays column-major. Flattening, reshaping and
tofilegive different orders. -
Views against copies. A NumPy slice is usually a view of the original data; a MATLAB slice is a new array.
-
Inclusive ranges.
1:3is[1 2 3], and the stop value is included. -
Everything is at least two-dimensional. A MATLAB array always has at least two dimensions, so there is no 1-D array; a vector is a row or a column.
MATLAB calls Python directly (see Calling Python from MATLAB).
From R
R shares MATLAB’s vector-first style but not its array model.
| Task | R | MATLAB |
|---|---|---|
Index base |
1 |
1 |
Negative index |
|
an error; use |
Assignment |
|
|
Matrix by columns |
|
|
Solve |
|
|
Missing values |
|
|
Data frame |
|
|
List |
|
cell array or structure |
Both languages store matrices column by column and both copy on modify. The vector in R has no dimension unless you give it one, while in MATLAB the dimension is always there. The R Reference (#282) has a section for MATLAB users that compares the two from the R side.
From Julia
Julia was designed to be familiar to MATLAB users, so most numerical code transfers with small edits.
| Task | Julia | MATLAB |
|---|---|---|
Index |
|
|
Element-wise operation |
|
|
Solve |
|
|
Function definition |
|
|
Multiple dispatch |
core feature |
not available; classes dispatch on the first argument (see Function Precedence and Name Resolution) |
Compilation |
JIT per method |
JIT inside the engine |
Package manager |
built in, |
MATLAB Package Manager since R2024b |
The practical differences are that Julia needs a dot to broadcast any function while most MATLAB built-ins are already array-aware, that Julia scripts are compiled per method signature, and that MATLAB is commercial while Julia is open source.
From Fortran
MATLAB is the nearest relative of Fortran 90 array syntax: column-major storage, 1-based indexing by default, array expressions and whole-array intrinsics. The heatflow problem is solved in Fortran in #277.
| Task | Fortran | MATLAB |
|---|---|---|
Declaration |
|
none; |
Whole-array arithmetic |
|
|
Matrix product |
|
|
Element-wise product |
|
|
Array section |
|
|
Loop |
|
|
Intent |
|
none; arguments are values, outputs are listed |
The biggest change is the loss of declarations and explicit memory control; the biggest gain is that the memory layout you wrote Fortran for is the one MATLAB already has. MATLAB can call compiled Fortran through MEX (see C/C++ Libraries and MEX).
From C
| Task | C | MATLAB |
|---|---|---|
Index |
|
|
Types |
declared |
inferred; |
Strings |
|
|
Memory |
manual |
managed; arrays grow on assignment |
Integer overflow |
wraps, or is undefined for signed types |
saturates ( |
Division of integers |
truncates |
rounds to the nearest integer |
Pointers |
yes |
none; handle objects are the nearest equivalent |
Logical and |
|
|
Integer arithmetic differs from C in an important way: MATLAB integer types saturate instead of wrapping, and int32(7)/int32(2) is 4, the rounded value, not 3. (Both results were produced in Octave 11.3.0 with int8(200) giving 127 and int32(7)/int32(2) giving 4; MATLAB documents the same saturation and rounding. See Numeric Types and Floating Point.
No +=: how MATLAB code is written instead
MATLAB has no compound assignment operators. The idioms are:
x = 5;
x = x + 1
Octave 11.3.0 output: x = 6. (Octave itself accepts +=; MATLAB does not, so code that uses it is not portable.)
Because the left side is an array, the same pattern covers accumulations: total = total + T(k). Growing an array in a loop, as in list(end+1) = value, works but reallocates each time and is flagged by the Code Analyzer; preallocate instead (see Vectorization and Preallocation).
Three operators that surprise newcomers
-
endmeans the last index inside parentheses, and also closes blocks.A(end, 1)is the first element of the last row. -
The quote
'is the conjugate transpose and.'the plain transpose. On complex data they differ. -
The backslash
\solves linear systems and least-squares problems:x = A\b.
z = [1+2i 3-4i];
z'
z.'
A = magic(3);
A(2:end, 1)
col = [1; 2; 3]; row = [10 20 30 40];
col + row
Octave 11.3.0 output:
ans =
1 - 2i
3 + 4i
ans =
1 + 2i
3 - 4i
ans =
3
4
ans =
11 21 31 41
12 22 32 42
13 23 33 43
z' conjugated and transposed, z.' only transposed. The last result is implicit expansion: adding a column to a row builds the whole table of sums, without repmat or the older bsxfun. It has worked since R2016b and is the subject of Implicit Expansion and Compatible Sizes.
Strengths and limits
Strengths
-
Dense numerical code is short and close to the mathematics.
-
Documentation, graphics and numerical libraries are integrated and consistent across releases, and backward compatibility is taken seriously.
-
The desktop supports exploration: a workspace to inspect, an editor with code analysis and a debugger.
Limits
-
Licensing. MATLAB is commercial software, and many capabilities that other ecosystems get from free libraries are separate paid toolboxes. This section documents base MATLAB only and lists the toolboxes on Toolboxes and Add-On Products.
-
General-purpose programming. The language is weaker than Python for text processing, web services and large software architectures, although classes, namespaces, a unit-testing framework and a package manager now exist.
-
Performance model. Fast code is vectorized code on dense arrays; algorithms that need millions of small irregular operations are better written in C++ and called through MEX.
-
Closed implementation. Behaviour is defined by the documentation and the product, not by a public specification.
GNU Octave in one line
GNU Octave is a free, open-source numerical language that is largely compatible with MATLAB, which is why the short sessions on these pages could be run in it; the differences are on GNU Octave Compatibility.
Where older books differ
Books written for the 2000s teach bsxfun and repmat for broadcasting (implicit expansion arrived in R2016b), character arrays as the only text type (string arrays arrived in R2016b), and numeric figure handles with set and get (graphics objects with dot notation appeared in R2014b, although set and get still work). The language described on this page is otherwise stable.
References
-
Avoid Unnecessary Copies of Data and How MATLAB Allocates Memory
-
Cleve Moler and Jack Little, A History of MATLAB, Proceedings of the ACM on Programming Languages 4 (HOPL), 2020
-
GNU Octave documentation (third-party, MATLAB-compatible)