isdiag - Determine if matrix is diagonal - MATLAB (original) (raw)
Main Content
Determine if matrix is diagonal
Syntax
Description
tf = isdiag([A](#bt6atid-1-A))
returns logical 1
(true
) if A
is a diagonal matrix. Otherwise, it returns logical 0
(false
).
Examples
Create a 4-by-4 identity matrix.
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.
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.
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
.
Test if B
is a diagonal matrix.
The matrix is diagonal because there are no nonzero elements above or below the main diagonal.
Input Arguments
Input array. isdiag
returns logical0
(false
) if A
has more than two dimensions.
Data Types: single
| double
| logical
Complex Number Support: Yes
More About
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.
Diagonal matrices are typically, but not always, square.
Tips
- Use the diag function to produce diagonal matrices for which
isdiag
returns logical1
(true
). - The functions
isdiag
,istriu
, andistril
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
The isdiag
function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced in R2014a