View all functions

CategoryArray: Shape
GPUYes

What does the diag function do in MATLAB / RunMat?

diag either constructs a diagonal matrix from a vector (placing the vector on a specified diagonal) or extracts a diagonal from a matrix. The behaviour matches MATLAB, including support for offsets, logical inputs, complex values, and character arrays.

How does the diag function behave in MATLAB / RunMat?

  • diag(v) with a vector v returns a square matrix whose main diagonal is v.
  • diag(v, k) places v on the k-th diagonal: super-diagonals for k > 0, sub-diagonals for k < 0. The output size grows by abs(k).
  • diag(A) with a matrix A returns a column vector containing the main diagonal of A.
  • diag(A, k) extracts the k-th diagonal. When the requested diagonal does not exist, an empty column vector is returned.
  • diag(v, 'vector') always returns a column vector copy of v, even when v is already a vector.
  • diag(v, m) forces an m × m square result, while diag(v, [m n]) creates an explicit rectangular size. You can combine both with offsets (e.g. diag(v, k, [m n])) when you need a wider diagonal band.
  • diag(___, 'logical') converts the result to a logical array. diag(___, 'double') forces a double-precision result when inputs are logical.
  • diag(___, 'like', prototype) matches the numeric flavour and residency of prototype (including GPU handles).
  • Logical inputs stay logical; complex inputs stay complex; character arrays preserve padding with spaces off the diagonal.
  • Higher-dimensional inputs are accepted when trailing dimensions are singleton—only the leading 2-D slice participates in the diagonal operation.

diag Function GPU Execution Behaviour

When the input lives on the GPU, RunMat calls the acceleration provider's diag_from_vector or diag_extract hook (see the GPU spec). Providers that do not expose these hooks fall back to a host round-trip: the input is gathered once, the diagonal computation runs on the CPU, and the result is uploaded back to the device. Size overrides or the 'vector' option also trigger the host path because they are not yet exposed through provider hooks. 'like' requests are honoured regardless of the path: GPU prototypes stay on the device, while logical or complex prototypes adjust the element type accordingly.

Examples of using the diag function in MATLAB / RunMat

Creating a diagonal matrix from a vector

v = [4 5 6];
D = diag(v);

Expected output:

D =
     4     0     0
     0     5     0
     0     0     6

Placing a vector on an upper diagonal

v = [1 2 3];
U = diag(v, 1);

Expected output:

U =
     0     1     0     0
     0     0     2     0
     0     0     0     3
     0     0     0     0

Extracting a subdiagonal as a column vector

A = [1 2 3; 4 5 6; 7 8 9];
d = diag(A, -1);

Expected output:

d =
     4
     8

Building a diagonal matrix from a logical mask

mask = logical([1 0 1 0]);
M = diag(mask);

Expected output:

M =
     1     0     0     0
     0     0     0     0
     0     0     1     0
     0     0     0     0

Keeping diagonal results on the GPU

G = gpuArray([2; 4; 8]);
D = diag(G);
firstTwo = gather(D(1:2, 1:2));

Expected output:

firstTwo =
     2     0
     0     4

Returning a vector without creating a matrix

v = [10 20 30];
d = diag(v, 'vector');

Expected output:

d =
    10
    20
    30

Creating a rectangular diagonal matrix with sz

v = [1 2];
R = diag(v, [2 4]);

Expected output:

R =
     1     0     0     0
     0     2     0     0

Matching residency and type with 'like'

G = gpuArray([1 3 5]');
D = diag([1 2 3], 'like', G);

The matrix D resides on the GPU and mirrors the numeric flavour of G. Subsequent GPU-friendly operations can consume D without additional transfers.

GPU residency in RunMat (Do I need gpuArray?)

You usually do NOT need to call gpuArray yourself in RunMat (unlike MATLAB).

In RunMat, the auto-offload planner keeps residency on the GPU when expressions make use of GPU providers. Even when the provider lacks diag_from_vector / diag_extract, the builtin gathers once on the host, performs the diagonal operation, and re-uploads the result so later GPU-friendly ops can continue without intervention.

To preserve backwards compatibility with MathWorks MATLAB, and for when you want to explicitly bootstrap GPU residency, you can call gpuArray to move data to the GPU. That mirrors MATLAB's behaviour while still allowing RunMat's planner to decide whether the GPU offers an advantage for the surrounding computation.

Since MathWorks MATLAB does not have a fusion planner, and they kept their parallel execution toolbox separate from the core language, as their toolbox is a separate commercial product, MathWorks MATLAB users need to call gpuArray to move data to the GPU manually whereas RunMat users can rely on the fusion planner to keep data on the GPU automatically.

FAQ

Does diag always return a square matrix?

Only when the input is a vector and you do not request otherwise. Use 'vector' to keep the result as a column vector, or pass a size vector (e.g. diag(v, [m n])) to create rectangular matrices.

What happens if I request a diagonal outside the matrix bounds?

You receive an empty column vector (size 0 × 0), matching MATLAB's behaviour.

Can I use diag with logical or character arrays?

Yes. Logical inputs produce logical outputs, and character inputs produce padded character arrays with spaces away from the diagonal.

Does diag support complex numbers?

Complex inputs are supported. The output keeps the real and imaginary parts intact.

How do offsets work with vectors?

diag(v, k) grows the matrix by abs(k) and shifts the diagonal up (k > 0) or down (k < 0).

Can I place a diagonal inside a non-square matrix?

No. MATLAB (and RunMat) always produces a square matrix when building from a vector.

What if the vector is empty?

diag([]) returns a 0 × 0 matrix. diag([], k) returns a square matrix of size abs(k) filled with zeros.

Do GPU results stay on the device?

Yes—providers with diag hooks operate entirely on the GPU. Providers without hooks perform a single host gather and upload, so downstream fused expressions still see a GPU handle. When you request 'like' with a GPU prototype, the result is uploaded back to the device even if a host fallback was required.

Is the offset argument required to be an integer?

Yes. Non-integer or non-finite offsets raise an error.

Does diag modify the original input?

No. It always returns a new array, leaving the input unchanged.

How do I match another array's type or residency?

Use the 'like' syntax: diag(v, 'like', prototype). Logical, complex, and GPU prototypes are respected even when the computation falls back to the CPU path.

Is single precision supported?

Not yet. Requesting 'single' currently raises an error. Use 'like' with an appropriate prototype once single-precision support lands.

See Also

eye, zeros, ones, gpuArray, gather

Source & Feedback