triu — Extract upper-triangular portions of matrices with optional diagonal offsets.
triu(A) keeps elements on and above a selected diagonal and sets elements below that diagonal to zero. Optional k selects the diagonal boundary using MATLAB-compatible semantics.
Syntax
B = triu(A)
B = triu(A, k)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Input scalar, matrix, or paged matrix array. |
k | NumericScalar | Yes | — | Diagonal offset for the preserved upper triangle. |
Returns
| Name | Type | Description |
|---|---|---|
B | Any | Upper triangular output array with preserved shape/type domain. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:triu:TooManyInputs | More than one optional argument is supplied after A. | triu: too many input arguments |
RunMat:triu:InvalidOffset | Diagonal offset is not a finite integer scalar value. | triu: invalid diagonal offset |
RunMat:triu:UnsupportedInput | Input value is not supported by triu. | triu: unsupported input type |
RunMat:triu:Internal | Internal masking/upload path fails. | triu: internal operation failed |
How triu works
- Works on numeric, logical, and complex arrays.
- Operates on the first two dimensions; trailing dimensions act as independent pages.
- Preserves logical types and complex-valued elements.
- Scalars are treated as
1×1matrices and honour diagonal offsets (for exampletriu(5, 1)returns0). - gpuArray inputs stay on the device when an acceleration provider supplies a native
triuhook; otherwise the runtime gathers, masks on the host, and uploads the result back to the GPU.
Does RunMat run triu on the GPU?
If the active acceleration provider implements a triu kernel the entire operation executes on the GPU.
Without a provider hook, RunMat gathers the tensor to host memory once, applies the mask, uploads the result, and returns a gpuArray so residency is preserved for downstream kernels.
Fallbacks never affect numerical results—only where the computation runs.
GPU memory and residency
G = gpuArray(rand(5));
U = triu(G, -2);
isa(U, 'gpuArray')Expected output:
ans =
logical
1Examples
Extracting the upper triangular part of a matrix
A = [1 2 3; 4 5 6; 7 8 9];
U = triu(A)Expected output:
U =
1 2 3
0 5 6
0 0 9Keeping one sub-diagonal beneath the main diagonal
A = magic(4);
U = triu(A, -1)Expected output:
U =
16 2 3 13
5 11 10 8
0 7 6 12
0 0 15 1Dropping the main diagonal with a positive offset
A = [1 2 3; 4 5 6; 7 8 9];
strict = triu(A, 1)Expected output:
strict =
0 2 3
0 0 6
0 0 0Applying triu to every page of a 3-D array
T = reshape(1:18, [3 3 2]);
U = triu(T)Expected output:
U(:, :, 1) =
1 2 3
0 5 6
0 0 9
U(:, :, 2) =
10 11 12
0 14 15
0 0 18Preserving gpuArray residency with triu
G = gpuArray(rand(5));
U = triu(G, -2);
isa(U, 'gpuArray')Expected output:
ans =
logical
1Using triu with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how triu changes the result.
Run a small triu example, explain the result, then change one input and compare the output.
FAQ
What happens when k is smaller than the matrix size (large negative)?⌄
The entire matrix is preserved; triu never removes elements above the chosen diagonal.
Does triu work with logical arrays?⌄
Yes. Elements below the retained diagonal become false, while the rest keep their logical values.
How are complex numbers handled?⌄
Each element retains its real and imaginary parts. Only elements below the chosen diagonal become 0 + 0i.
What about empty matrices or zero-sized dimensions?⌄
triu returns an array of the same shape, leaving all entries at zero. Trailing dimensions with size zero are treated as empty batches.
Does triu change the class of character arrays?⌄
Character arrays are converted to their numeric codes (double precision) before the triangular mask is applied, matching MATLAB behaviour.
Related Array functions
Shape
cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · squeeze · tril · vertcat
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how triu is executed, line by line, in Rust.
- View the source for triu in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 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.