Matrix languages · interactive use
Working at a Matrix Command Prompt: Entering Code and Reading Results
A matrix command prompt is an interactive loop: you type a statement, the system evaluates it, prints the result and waits for the next one. Variables persist from one line to the next in a workspace, so a session builds up state the way a notebook does. This style of working, often called a read-evaluate-print loop, suits numerical exploration because you can look at an intermediate matrix before deciding what to do with it.
Entering matrices
A matrix literal lists rows separated by semicolons and entries separated by spaces or commas. Typing A = [1 2; 3 4] creates a 2 × 2 matrix named A and echoes it back. Ranges generate vectors: 0:0.5:2 gives 0, 0.5, 1, 1.5, 2, and the generator functions zeros(3), ones(2, 4) and eye(3) build the common special matrices. A transposed copy comes from the apostrophe, so A’ is the transpose of A (conjugate transpose, if the entries are complex; see complex conjugate).
The result of an expression that is not assigned goes into a default variable, usually named ans. Typing 2*A with no name on the left shows the doubled matrix and stores it in ans, which the next line can use. That is convenient for a quick check and a bad habit in anything you intend to keep.
Output control
Echoing every result gets noisy once a matrix has a thousand rows. A trailing semicolon suppresses printing: x = rand(1000, 1000); creates the matrix silently. Leaving the semicolon off is a useful tool in the opposite direction, since it makes a statement display its value, which is the cheapest debugging technique there is.
Display format is separate from stored precision. Showing four decimal places does not round the stored value, and a value that prints as 0.3333 is still the full double-precision number underneath. Commands that change the display format, to show more digits or switch to scientific notation, change only how the number looks.
The workspace
Every name you create lives in the workspace until you remove it. A list command shows the variables with their sizes, which catches the common mistake of reusing a name for a matrix of the wrong shape. A clear command removes variables, and a size query such as size(A) returns the row and column counts, here [2 2]. Check sizes before operations that need them to agree, because most error messages at the prompt are dimension mismatches.
One habit saves hours: name matrices by role, not by letter, once a session passes a dozen variables. A session full of A, B, C and ans looks fine for ten minutes and is unreadable afterwards.
Scripts: saving what worked
Once a sequence of statements does what you want, copy it into a text file and run the file. A script is exactly that: the same statements, executed in the same workspace, so it can read and overwrite any variable already there. Scripts are right for a one-off analysis and wrong for reusable calculations, because their behavior depends on whatever happens to be in the workspace when they start.
A function fixes that. It has its own private workspace, takes arguments and returns values, so calling it cannot disturb anything outside. The shape is the same in most matrix languages:
function [mu, sigma] = describe(x)
mu = sum(x) / length(x);
sigma = sqrt(sum((x - mu).^2) / (length(x) - 1));
end
Calling describe([2 4 4 4 5 5 7 9]) returns a mean of 5 and a sample standard deviation of about 2.138. The squared deviations sum to 9 + 1 + 1 + 1 + 0 + 0 + 4 + 16 = 32, and 32/7 ≈ 4.571, whose square root is about 2.138. The statement x - mu subtracts a scalar from a whole vector, which is vectorization at work.
Error messages and how to read them
Messages at a matrix prompt are short, and they name the failing operation. A message about inner dimensions means a matrix product was attempted between shapes that do not line up, say a 3 × 2 times a 3 × 2, and the cure is usually a missing transpose. An index exceeding the array bounds points at an off-by-one or a stale variable, as in elements of a matrix. A singular-matrix warning after a solve means the answer cannot be trusted; revisit the conditioning of the system under matrix division.
A reasonable workflow
Explore at the prompt, then move working lines into a script, and move anything used twice into a function. Keep the script short enough to read in one screen. Save intermediate results to a file when a calculation takes long, so that a crash costs a reload instead of a rerun.
For the ideas behind the interactive style, see what a matrix language is. Tooling choices differ between systems, and the comparison in MATLAB-style matrix languages compared covers the main ones. The linear algebra hub lists the method pages that put this to use.