Language reference
Array queries
Reshape typed arrays and inspect shapes, nonzero coordinates and logical reductions.
Shape and length
size(A) returns [rows, columns]; size(A, dim) returns one dimension and dimensions above 2 return 1. dim must be a positive, exactly representable scalar integer. [rows, cols] = size(A) returns dimensions separately; additional outputs up to 32 are 1. size(A, dim) accepts only one output.
length returns the largest dimension of a nonempty array, 1 for a scalar and 0 for any empty shape. numel counts elements; isempty tests whether that count is zero.
A = [1, 2, 3; 4, 5, 6];
[rows, cols] = size(A);
disp(size(A, 3));
disp(length(A));
disp(numel(A));Reshape in column-major order
reshape(A, rows, cols) and reshape(A, [rows, cols]) preserve real, logical or complex type and the column-major element sequence. A row or column size vector is accepted. The output is an independent, contiguous 2D array; reshaping a scalar produces a 1-by-1 array.
Dimensions must be finite nonnegative integers within the array bound. One [] dimension can be inferred when the other is positive and divides the element count exactly. Explicit zero dimensions preserve empty shapes; inferring against a zero known dimension is rejected. N-D reshaping is not supported.
A = [1, 2, 3; 4, 5, 6];
B = reshape(A, 3, []);
disp(B);
mask = reshape([true, false, true, false], [2, 2]);Nonzero indices and values
find(A) returns 1-based linear indices in column-major order. find(A, count, "first"|"last") limits matches; count is a nonnegative integer up to 16777216. Last selects from the end but still returns ascending indices.
[row, col] or [row, col, value] = find(A) returns coordinates and optionally typed values. A row-shaped input yields row outputs; other arrays yield columns. Current empty conventions include find([]) as 0-by-1 and find(0) as 1-by-0.
Real, logical and complex values are supported. Either nonzero complex component counts; NaN and infinities count as nonzero and signed zeros do not.
A = [0, 2; 3, 0];
indices = find(A);
[row, col, value] = find(A);
last = find(A, 1, "last");
disp(A(indices));Any versus all
any(A) reduces the first non-singleton dimension; any(A, 1|2) selects an axis and any(A, "all") returns one global boolean. Default vector/scalar results are booleans, explicit array reductions are logical arrays, and empty reductions use false while preserving reduced axes.
any accepts real, logical and complex data. all(A) still performs only a global reduction; it does not yet accept dimensions or "all". Shape operations, find and any are pure single-output expressions in parfor; multiple-target assignments are not allowed there.
A = [0, 2; 0, 0];
disp(any(A));
disp(any(A, 2));
disp(any(A, "all"));
disp(all(A));