Language reference
Matrices and indexing
Matrix literals, 1-based indexing, ranges, slices, and shape inspection.
01
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)); % 402
1-based indexing
A(row, column) addresses a matrix element. A(index) uses 1-based column-major order. end resolves to the active axis length or total element count.
A = [1, 2, 3; 4, 5, 6];
disp(A(end, 1));
disp(A(1, end));
A(end, end) = 99;03
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];04
Matrix and element-wise operations
* performs matrix multiplication while .* multiplies compatible values element by element. ./ is element-wise division. The interpreter also supports compatible 2D singleton broadcasting for addition and element-wise multiplication.