diag — Create diagonal matrices or extract diagonals in MATLAB and RunMat.
diag either constructs a diagonal matrix from a vector or extracts a diagonal from a matrix. Offset handling and supported input classes follow MATLAB semantics.
Syntax
B = diag(A)
B = diag(A, k)
B = diag(A, sz)
B = diag(A, k, sz)
B = diag(A, "vector")
B = diag(A, class)
B = diag(A, "like", prototype)
B = diag(A, args...)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Input scalar, vector, or matrix. |
k | NumericScalar | Yes | — | Diagonal offset index. |
sz | SizeArg | Yes | — | Output matrix size override as [m n] or scalar n. |
option | StringScalar | Yes | "vector" | Vector extraction option ('vector'). |
class | StringScalar | Yes | — | Output class override ('logical' or 'double'). |
like | StringScalar | Yes | "like" | Literal option name ('like'). |
prototype | LikePrototype | Yes | — | Prototype value controlling output class/residency. |
args | Any | Variadic | — | Optional offset/size/options parsed by diag argument grammar. |
Returns
| Name | Type | Description |
|---|---|---|
B | Any | Diagonal matrix or diagonal vector extracted from the input. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:diag:InvalidInput | Input type, option grammar, size override, or output conversion is invalid. | diag: invalid input argument |
RunMat:diag:InvalidOffset | Diagonal offset is not a finite integer scalar. | diag: invalid diagonal offset |
How diag works
diag(v)with a vectorvreturns a square matrix whose main diagonal isv.diag(v, k)placesvon thek-th diagonal: super-diagonals fork > 0, sub-diagonals fork < 0. The output size grows byabs(k).diag(A)with a matrixAreturns a column vector containing the main diagonal ofA.diag(A, k)extracts thek-th diagonal. When the requested diagonal does not exist, an empty column vector is returned.diag(v, 'vector')always returns a column vector copy ofv, even whenvis already a vector.diag(v, [m n])creates an explicit rectangular size. You can combine it 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 ofprototype(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.
Does RunMat run diag on the GPU?
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.
GPU memory and residency
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.
Examples
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 6Placing 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 0Extracting a subdiagonal as a column vector
A = [1 2 3; 4 5 6; 7 8 9];
d = diag(A, -1)Expected output:
d =
4
8Building 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 0Keeping 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 4Returning a vector without creating a matrix
v = [10 20 30];
d = diag(v, 'vector')Expected output:
d =
10
20
30Creating a rectangular diagonal matrix with sz
v = [1 2];
R = diag(v, [2 4])Expected output:
R =
1 0 0 0
0 2 0 0Matching residency and type with 'like'
G = gpuArray([1 3 5]');
D = diag([1 2 3], 'like', G)Using diag with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how diag changes the result.
Run a small diag example, explain the result, then change one input and compare the output.
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?⌄
Yes. Pass an explicit size vector such as diag(v, [m n]), and optionally combine it with an offset via diag(v, k, [m n]).
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.
Related Array functions
Shape
cat · circshift · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · squeeze · tril · triu · vertcat
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how diag is executed, line by line, in Rust.
- View the source for diag 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.