Matrix languages · foundations
What Is a Matrix Language? Arrays, Operators and Vectorization
A matrix language is a programming language whose basic value is a rectangular array of numbers. Where a general-purpose language sees a loop over indices, a matrix language sees one statement acting on a whole array. Writing C = A*B multiplies two matrices; writing x = A\b solves a linear system. The code reads like the mathematics, and that is the point.
The core idea: one value, many numbers
In most languages a number is a number and a collection of numbers is a library type you manage by hand. In a matrix language the scalar is just a 1 × 1 matrix, a vector is a 1 × n or n × 1 matrix, and every operation is defined for arrays of any size. There is no separate vector type to convert to and from.
That choice has consequences. Types are mostly numeric and mostly floating point, with complex numbers built in. Dimensions are part of the value, so a dimension mismatch is a runtime error with a clear meaning. And the language ships with the standard operations of numerical linear algebra, such as transpose, inverse, determinant, factorizations and eigenvalues, as built-in functions rather than add-ons.
Operators with mathematical meaning
Two families of operators carry most of the weight. The matrix operators follow the rules of linear algebra: * is the matrix product, and the backslash solves Ax = b, as explained under matrix division. The dotted operators, .* and ./ and .^, apply the operation to matching entries, as covered under element-by-element multiplication.
Keeping the two apart is the most important habit to learn. Both are legal on square arrays of the same size, both run without error, and they give different answers. Beyond them, comparison operators produce arrays of true and false values, which in turn can index other arrays: x(x > 0) picks the positive entries.
Vectorization instead of loops
Numerical programs in a matrix language are written, wherever possible, as array expressions. Suppose you want the squares of a million samples. The loop version visits each index in the interpreter. The array version, x.^2, hands the whole job to compiled code in one call.
The speed difference comes from where the work happens, not from any cleverness in the arithmetic. Interpreter overhead is paid once per statement instead of once per element. A rule of thumb follows: if you find a loop that only combines entries independently, there is probably an array expression that replaces it. Loops that depend on the previous iteration, like a recurrence, or whose body is complicated, often stay loops. A running product, for instance, has a built-in, cumulative product, but a custom recurrence may not.
Getting data in and out
A matrix language is only as useful as its connection to data. Typical needs are reading a delimited text file into a matrix, writing results back, and reading binary files with a known layout. Delimited text is the common exchange format: one row of the file becomes one row of the matrix, with a separator between entries. Missing values show up as NaN, and unequal row lengths are an error, since a matrix must be rectangular.
Plotting is built in, usually with a single call per figure type: line plots, scatter plots, histograms and surface or contour plots. The same array that feeds a calculation feeds the plot, with no conversion step.
A small program, end to end
t = 0:0.1:1; % 11 sample times
y = 2*t + 1; % a line, vectorized
noise = [0.1 -0.2 0.05 0 0.1 -0.1 0.2 -0.05 0 0.1 -0.1];
z = y + noise; % add measurement error
A = [ones(11,1), t']; % design matrix for a straight-line fit
c = A \ z'; % least squares: intercept and slope
The last line is a linear least-squares fit, described in left division and least squares. In five statements it generates data, perturbs it and recovers the line’s coefficients. Expect an intercept near 1 and a slope near 2, shifted slightly by the noise. In a language without array types, the same task takes explicit loops and a hand-coded solver.
Strengths and limits
The strengths are clarity and speed of exploration. A formula from a textbook converts to a line of code with little translation, and results can be inspected and plotted instantly. The limits are equally real. Scalar-heavy code with irregular control flow runs slowly in interpreters, memory use grows with every temporary array, and a matrix of numbers is an awkward container for strings or records.
Several mature matrix languages exist, and their similarities and differences are laid out in MATLAB-style matrix languages compared. A hands-on view of the interactive side is in working at a matrix command prompt. Terms such as vectorization and broadcasting appear in the glossary, and the linear algebra hub lists the method pages built on these foundations.