Language reference
Matrices and indexing
Matrix literals, 1-based indexing, ranges, slices, and shape inspection.
Matrix literals
Commas separate columns and semicolons or newlines separate rows. Rows must have equal lengths.
A = [1, 2, 3;
4, 5, 6];
disp(size(A)); % [2, 3]
disp(A(2, 1)); % 41-based indexing
A(row, column) addresses an array element. A(index) uses 1-based column-major order. end resolves to the active axis length or total element count.
Logical masks must match the selected dimension. Linear masks return a column in column-major order and can be used for validated assignment.
A = [1, 2, 3; 4, 5, 6];
mask = A >= 3;
selected = A(mask);
A(mask) = 0;
disp(A(end, 1));Slices
Bare : selects an axis. Integer ranges support start:stop and start:step:stop. Reads create independent matrices; writes require an existing target.
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
rows = A(2:end, :);
last = A(:, end);
A(1:2, end) = [30; 60];Matrix and element-wise operations
* performs real matrix multiplication; .*, ./ and .^ operate element by element with compatible 2D broadcasting. Real comparisons return logical arrays. Complex matrix multiplication, ordered comparison, solve and decompositions other than eig remain unsupported. FFT and real/complex eig/roots are available; see /language/spectral-statistics.
sum(A) and mean(A) now reduce along the first non-singleton dimension. Use an explicit "all" for a global statistic. See Reductions and migration for the changed defaults.
A = [1, 2, 3; 4, 5, 6];
mask = A > mean(A, "all");
z = [1+i, 2-i];
energy = z .* conj(z);Index-vector orientation
Numeric column indices into a matrix produce a column, so A(find(A)) follows find's column-major order. When the source is itself a vector, numeric selection keeps the source vector's orientation. Colon and logical-mask selection keep their existing column-major rules.
A = [0, 2; 3, 0];
indices = find(A);
values = A(indices);
disp(values);