Language reference
Reductions and migration
Use dimensional defaults, explicit global reductions and sample or population statistics.
First non-singleton dimension
sum, mean, prod, median, min, max, var and std now reduce the first non-singleton dimension by default. A regular 2D matrix reduces down columns and returns a row. A row or column vector reduces to a scalar number.
Explicit dimension 1 or 2 preserves array shape, including 1-by-1 arrays. Scalar inputs remain scalar. Numerical reductions accept real and logical values, convert logical inputs to numbers and propagate NaN; complex numerical reductions are not supported.
A = [1, 2, 3; 4, 5, 6];
disp(sum(A)); % [5, 7, 9]
disp(mean(A, 2)); % [2; 5]
disp(sum([1, 2])); % 3Explicit global statistics
Use "all" when a statistic should cover every element. min/max require the empty second argument to distinguish a reduction from pairwise comparison.
A = [1, 2, 3; 4, 5, 6];
disp(sum(A, "all"));
disp(mean(A, "all"));
disp(prod(A, "all"));
disp(median(A, "all"));
[largest, index] = max(A, [], "all");Sample and population normalization
var/std(A) use sample normalization N-1. The second argument is normalization: 0 or [] means sample, 1 means population N. An optional third argument selects dimension 1, dimension 2 or "all". var/std(A, "all") are sample-statistic shorthands.
Migration is required: old var(A, dim) and std(A, dim) become var(A, 0, dim) and std(A, 0, dim). In particular, var(A, 1) now means population variance along the default axis, not sample variance along dimension 1.
A = [1, 2, 3; 4, 5, 6];
sample = var(A, 0, 2);
population = std(A, 1, 2);
globalSample = std(A, 0, "all");Older global-default code must add "all" explicitly. Review saved scripts, CSV summaries and comparisons against mean(A); broadcasting can make an outdated expression run with a different meaning.
Extrema and indices
[values, indices] = min/max(A) uses the default axis; min/max(A, []) is equivalent. Add a third dimension or "all" for explicit reductions. Indices are 1-based, ties choose the first occurrence and a propagated NaN uses its first position. Global indices are column-major linear indices.
min/max(A, B) remains a pairwise broadcast comparison with one output only. Numerical extrema of logical inputs return numbers, not logical arrays.
A = [4, 2; 4, 9];
[values, rows] = max(A);
[smallest, linearIndex] = min(A, [], "all");
pairwise = max(A, 5);Empty inputs
- sum([]) is 0 and prod([]) is 1.
- mean, median, var and std of [] produce NaN.
- min/max of [] remain empty.
- Explicit dimensional reductions preserve the corresponding reduced empty shapes. Do not infer every empty shape from the scalar identities.