Main Content

isdiag

Determine if matrix is diagonal

Description

example

tf = isdiag(A) returns logical 1 (true) if A is a diagonal matrix. Otherwise, it returns logical 0 (false).

Examples

collapse all

Create a 4-by-4 identity matrix.

I = eye(4)
I = 4×4

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1

Test if the matrix is diagonal.

isdiag(I)
ans = logical
   1

The matrix is diagonal because all of the nonzero elements are on the main diagonal.

Create a matrix with nonzero elements on the main and first diagonals.

A = 3*eye(4) + diag([2 2 2],1)
A = 4×4

     3     2     0     0
     0     3     2     0
     0     0     3     2
     0     0     0     3

Test if the matrix is diagonal.

isdiag(A)
ans = logical
   0

The matrix is not diagonal because there are nonzero elements above the main diagonal.

Create a new matrix, B, from the main diagonal elements of A.

B = diag(diag(A));

Test if B is a diagonal matrix.

isdiag(B)
ans = logical
   1

The matrix is diagonal because there are no nonzero elements above or below the main diagonal.

Input Arguments

collapse all

Input array. isdiag returns logical 0 (false) if A has more than two dimensions.

Data Types: single | double | logical
Complex Number Support: Yes

More About

collapse all

Diagonal Matrix

A matrix is diagonal if all elements above and below the main diagonal are zero. Any number of the elements on the main diagonal can also be zero.

For example, this 4-by-4 identity matrix is a diagonal matrix.

I4=(1000010000100001)

Diagonal matrices are typically, but not always, square.

Tips

  • Use the diag function to produce diagonal matrices for which isdiag returns logical 1 (true).

  • The functions isdiag, istriu, and istril are special cases of the function isbanded, which can perform all of the same tests with suitably defined upper and lower bandwidths. For example, isdiag(A) == isbanded(A,0,0).

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2014a