Accelerating the pace of engineering and science

# 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

expand all

### Test Diagonal Matrix

Create a 4-by-4 identity matrix.

`I = eye(4)`
```I =

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 =

1```

The result is logical 1 (true) because all of the nonzero elements in I are on the main diagonal.

### Test Banded Matrix

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

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

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 =

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 =

1```

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

## Input Arguments

expand all

### A — Input arraynumeric array

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

expand 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,

${I}_{4}=\left(\begin{array}{cccc}1& 0& 0& 0\\ 0& 1& 0& 0\\ 0& 0& 1& 0\\ 0& 0& 0& 1\end{array}\right)$

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