tril — Extract the lower-triangular part of each matrix page.
tril(A) preserves the main diagonal and entries below it. tril(A,k) shifts the retained diagonal by the finite integer offset k.
Syntax
B = tril(A)
B = tril(A, k)Inputs
A- Type
Any- Required
- Yes
- Description
- Dense numeric, logical, character, complex, or resident array.
k- Type
NumericScalar- Required
- Yes
- Description
- Finite integer-valued diagonal offset.
Returns
B- Type
Any- Description
- Lower-triangular output with the input class and shape.
Errors
RunMat:tril:TooManyInputs- When
- More than one optional argument is supplied after A.
- Message
tril: too many input arguments
RunMat:tril:TooManyOutputs- When
- More than one output is requested.
- Message
tril: too many output arguments
RunMat:tril:InvalidOffset- When
- The diagonal offset is not a finite integer-valued real scalar in the platform index range.
- Message
tril: invalid diagonal offset
Behavior
The output has the same full shape as A; arrays with more than two dimensions are masked independently page by page.
Positive k retains superdiagonals and negative k removes diagonals at and above the shifted boundary.
Dense single, double, logical arrays, character, complex floating, and all eight native integer classes are supported. Array class and precision are preserved; a scalar logical is normalized to the runtime's double scalar representation, and masked character entries become the NUL character.
Native complex-integer components are structurally masked with a same-class zero. Strings, cells, sparse arrays, structures, objects, symbolic values, and callable/execution carriers are rejected.
k may be a real floating, logical, or native integer scalar. Native integers are decoded exactly; nonfinite, fractional, nonscalar, complex, and out-of-platform-range offsets are rejected.
Zero requested outputs still validates arguments and may execute the operation. More than one requested output is rejected before masking or provider access.
GPU and execution
Resident floating input first uses the owning provider's tril operation. If that hook is unavailable, RunMat gathers once, masks on the host, and restores through that same provider.
Resident integer input uses exact gather/mask/restoration because the floating provider hook is not an integer authority. If no owner provider is available, automatic residency may produce a host result.
The optional offset is validated after an admitted gather when resident. Triangular masking is a fusion boundary and does not replay an operational provider failure as a second numerical operation.
Examples
Main and lower diagonals
A = [1 2 3; 4 5 6; 7 8 9];
L = tril(A)Expected output:
L = [1 0 0; 4 5 0; 7 8 9]Shift the retained diagonal
A = reshape(1:9,3,3);
L = tril(A,-1);Keep one superdiagonal
A = magic(4);
L = tril(A,1);Drop the main diagonal
A = [1 2 3;4 5 6;7 8 9];
strict = tril(A,-1);Mask every matrix page
T = reshape(1:18,[3 3 2]);
L = tril(T);Preserve exact integer pages
wide = 0x0020000000000001u64; A = reshape(uint64([1 2 3 4 wide 6 7 8]),2,2,2); L = tril(A);Keep a resident single matrix resident
G = gpuArray(single([1 2;3 4])); L = tril(G);Preserve residency with an offset
G = gpuArray(single(reshape(1:25,5,5)));
L = tril(G,-2);Using tril with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how tril changes the result.
Run a small tril example, explain the result, then change one input and compare the output.
FAQ
What happens when k is larger than the matrix size?⌄
The entire matrix is preserved; tril never removes elements below the chosen diagonal.
Does tril work with logical arrays?⌄
Yes. Elements above the retained diagonal become false, while the rest keep their logical values.
How are complex numbers handled?⌄
Each retained element keeps its real and imaginary parts. Masked elements become zero in both components.
What about empty matrices or zero-sized dimensions?⌄
tril returns an array of the same shape. Trailing zero-sized dimensions remain empty pages.
Does tril change the class of character arrays?⌄
No. Character arrays remain character arrays; masked entries become NUL characters.
Does tril flatten N-D input?⌄
No. It preserves every dimension and applies the same row/column mask independently to each trailing matrix page.
Does tril convert integer data to double?⌄
No. Native integer data and typed zeros retain the exact input class, including values not exactly representable as binary64.
Related Array functions
Shape
blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · shiftdim · squeeze · toeplitz · triu · vertcat
Accumulation
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Creation
colon · createArray · empty · eye · false · full · inf · int16.empty · int32.empty · int64.empty · int8.empty · linspace · logspace · magic · meshgrid · nan · ndgrid · nonzeros · ones · peaks · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · uint16.empty · uint32.empty · uint64.empty · uint8.empty · zeros
Combinatorics
Binning
Grouping
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.