# isdiag

Determine if a matrix is a diagonal matrix

### isdiag(A)

• A is an array, which can be real or complex.
• If all off-diagonal elements of A are zeros, it returns logical 1.
• When A is complex, it returns logical 1 if both isdiag(imag(A)) and isdiag(real(A)) are true.
• If A is empty, it returns logical 1.

Example 1:

% All off-diagonal elements are zeros.
a=[diag(rand(1,3)) zeros(3,2)]
isdiag(a)

% All off-diagonal elements are zeros.
a=zeros(3,4)
isdiag(a)

% Not diagonal
a=diag(rand(1,3),1)
isdiag(a)

% Not diagonal
a=[diag(rand(1,3)) eye(3,2)]
isdiag(a)

a = 1e-1 ×
6.474   0.000   0.000   0.000   0.000
0.000   0.358   0.000   0.000   0.000
0.000   0.000   5.130   0.000   0.000

ans =
1

a =
0.000   0.000   0.000   0.000
0.000   0.000   0.000   0.000
0.000   0.000   0.000   0.000

ans =
1

a = 1e-1 ×
0.000   5.431   0.000   0.000
0.000   0.000   8.791   0.000
0.000   0.000   0.000   5.783
0.000   0.000   0.000   0.000

ans =
0

a =
0.254   0.000   0.000   1.000   0.000
0.000   0.502   0.000   0.000   1.000
0.000   0.000   0.420   0.000   0.000

ans =
0