RunMat
GitHub

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

NameTypeRequiredDefaultDescription
AAnyYesInput scalar, matrix, or paged matrix array.
kNumericScalarYesDiagonal offset for the preserved upper triangle.

Returns

NameTypeDescription
BAnyUpper triangular output array with preserved shape/type domain.

Errors

IdentifierWhenMessage
RunMat:triu:TooManyInputsMore than one optional argument is supplied after A.triu: too many input arguments
RunMat:triu:InvalidOffsetDiagonal offset is not a finite integer scalar value.triu: invalid diagonal offset
RunMat:triu:UnsupportedInputInput value is not supported by triu.triu: unsupported input type
RunMat:triu:InternalInternal 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×1 matrices and honour diagonal offsets (for example triu(5, 1) returns 0).
  • gpuArray inputs stay on the device when an acceleration provider supplies a native triu hook; 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
   1

Examples

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     9

Keeping 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     1

Dropping 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     0

Applying 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    18

Preserving gpuArray residency with triu

G = gpuArray(rand(5));
U = triu(G, -2);
isa(U, 'gpuArray')

Expected output:

ans =
  logical
   1

Using 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.

Shape

cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · squeeze · tril · vertcat

Sorting Sets

argsort · intersect · ismember · issorted · setdiff · sort · sortrows · union · unique

Creation

colon · eye · false · fill · linspace · logspace · magic · meshgrid · ones · peaks · rand · randi · randn · randperm · range · true · zeros

Indexing

find · ind2sub · sub2ind

Introspection

isempty · ismatrix · isscalar · isvector · length · ndims · numel · size

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how triu is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.