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 to see if the matrix is diagonal.

isdiag(I)
ans = logical
   1

The result is logical 1 (true) because all of the nonzero elements in I 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 to see if the matrix is diagonal.

isdiag(A)
ans = logical
   0

The matrix is not diagonal since 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 to see if B is a diagonal matrix.

isdiag(B)
ans = logical
   1

The result is logical 1 (true) because there are no nonzero elements above or below the main diagonal of B.

Input Arguments

collapse all

Input array, specified as a numeric array. isdiag returns logical 0 (false) if A has more than two dimensions.

Data Types: single | double
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, the 4-by-4 identity matrix,

I4=(1000010000100001)

is a diagonal matrix. 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