isdiag(A) -> Bool

Test whether a matrix is diagonal.


  1. Check if a matrix is diagonal:

    julia> A = [1 0 0; 0 2 0; 0 0 3];
    julia> isdiag(A)

    This example checks if the matrix A is diagonal. In this case, it returns true because all the off-diagonal elements are zero.

  2. Handle non-diagonal matrices:

    julia> B = [1 2 3; 4 5 6; 7 8 9];
    julia> isdiag(B)

    It returns false because the matrix B has non-zero elements outside the diagonal.

  3. Check if a square matrix is empty:
    julia> C = zeros(0, 0);
    julia> isdiag(C)

    It correctly handles the case when the square matrix C is empty, and returns true since there are no off-diagonal elements to consider.

Common mistake example:

julia> D = [1 0; 0 2; 0 0];
julia> isdiag(D)
ERROR: DimensionMismatch("matrix must be square")

In this example, the matrix D provided is not square, resulting in a DimensionMismatch error. Ensure that the matrix passed to isdiag is a square matrix to avoid this error.

See Also

Ac_ldiv_B, Ac_ldiv_Bc, Ac_mul_B, Ac_mul_Bc, Ac_rdiv_B, Ac_rdiv_Bc, At_ldiv_B, At_ldiv_Bt, At_mul_B, At_mul_Bt, At_rdiv_B, At_rdiv_Bt, A_ldiv_Bc, A_ldiv_Bt, A_mul_B!, A_mul_Bc, A_mul_Bt, A_rdiv_Bc, A_rdiv_Bt, Bidiagonal, cond, conv2, det, diag, diagind, diagm, diff, eig, eigvals, eigvecs, expm, eye, full, inv, isdiag, ishermitian, isposdef, isposdef!, issym, istril, istriu, logabsdet, logdet, lyap, norm, qrfact, rank, repmat, rot180, rotl90, rotr90, sortrows, sqrtm, SymTridiagonal, trace, Tridiagonal, tril, tril!, triu, triu!, writedlm,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field

Checking you are not a robot: