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

NameTypeRequiredDefaultDescription
AAnyYesInput numeric, logical, complex, sparse, or gpuArray matrix.

Returns

NameTypeDescription
tfLogicalArrayTrue when the input matrix satisfies the requested structure predicate.

Errors

IdentifierWhenMessage
RunMat:isdiag:InvalidInputInput cannot be interpreted as a supported numeric, logical, sparse, complex, or gpuArray matrix.isdiag: invalid input
RunMat:isdiag:InternalRuntime 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
   1

Detecting an off-diagonal value

B = [1 0; 7 2];
tf = isdiag(B)

Expected output:

tf = logical
   0

Checking a rectangular diagonal matrix

C = [1 0; 0 2; 0 0];
tf = isdiag(C)

Expected output:

tf = logical
   1

Inspecting a GPU-resident matrix

G = gpuArray(eye(3));
tf = isdiag(G)

Expected output:

tf = logical
   1

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

Factor

chol · decomposition · eig · eigs · lu · qr · svd

Solve

cond · det · inv · linsolve · norm · null · pinv · rank · rcond · rref · vecnorm

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how isdiag 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.