isdiag — Determine whether a matrix is diagonal in MATLAB and RunMat.
isdiag(A) returns true when every nonzero element of matrix A lies on the main diagonal.
Syntax
tf = isdiag(A)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Input numeric, logical, complex, sparse, or gpuArray matrix. |
Returns
| Name | Type | Description |
|---|---|---|
tf | LogicalArray | True when the input matrix satisfies the requested structure predicate. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:isdiag:InvalidInput | Input cannot be interpreted as a supported numeric, logical, sparse, complex, or gpuArray matrix. | isdiag: invalid input |
RunMat:isdiag:Internal | Runtime fails while gathering GPU input or constructing an internal matrix view. | isdiag: internal runtime failure |
How isdiag works
- Scalars are diagonal.
- Rectangular matrices can be diagonal when every off-diagonal entry is zero.
- All-zero and empty matrices are diagonal.
- Dense real, logical, complex, sparse, and gpuArray inputs are supported.
- Complex entries count as nonzero when either real or imaginary part is nonzero.
- NaN values count as nonzero when testing matrix structure.
- Arrays with a non-singleton dimension beyond the second return
false, matching MATLAB matrix-predicate behavior.
Does RunMat run isdiag on the GPU?
isdiag is registered as a structure-analysis fusion sink. GPU-resident real/logical inputs use provider bandwidth (lower == 0 && upper == 0) instead of gathering the full matrix when the active provider supports it.
GPU memory and residency
The result is always a host logical scalar. For real/logical gpuArray inputs, RunMat keeps the matrix on the provider and uses the bandwidth hook to read back only lower/upper bandwidth metadata. Complex-interleaved GPU inputs and providers without bandwidth support fall back to the host path.
Examples
Checking a diagonal matrix
A = [4 0 0; 0 5 0; 0 0 6];
tf = isdiag(A)Expected output:
tf = logical
1Detecting an off-diagonal value
B = [1 0; 7 2];
tf = isdiag(B)Expected output:
tf = logical
0Checking a rectangular diagonal matrix
C = [1 0; 0 2; 0 0];
tf = isdiag(C)Expected output:
tf = logical
1Inspecting a GPU-resident matrix
G = gpuArray(eye(3));
tf = isdiag(G)Expected output:
tf = logical
1Using isdiag with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how isdiag changes the result.
Run a small isdiag example, explain the result, then change one input and compare the output.
FAQ
Does isdiag require a square matrix?⌄
No. A rectangular matrix is diagonal when all entries off the main diagonal are zero.
How are sparse matrices handled?⌄
RunMat scans stored sparse entries without densifying and returns false only when a stored nonzero appears off the main diagonal.
Does NaN count as zero?⌄
No. A NaN entry is treated as nonzero for structure predicates.
Related Linalg functions
Structure
bandwidth · ishermitian · issymmetric · istril · istriu · symrcm
Ops
cross · ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how isdiag is executed, line by line, in Rust.
- View the source for isdiag 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.